ofxTCPServer
The ofxTCPServer creates a TCP server that will serve up TCP data to any client that can reach it. To set it up you create an instance of an ofxTCPServer and call setup() passing the port number that you want your server to listen on:
TCP.setup(8080);
Clients connect to the server and get assigned a unique ID that allows you to send or receive data from them.
That unique ID is important because when a client disconnects, its ID isn't recycled, the 100th client to connect will be 100, even if there are only currently 2 clients connected.
You can check to see how many clients are connected to your server using the getNumClients() method, but to loop through the clients, you'll want to do something like the following:
for(int i = 0; i < TCP.getLastID(); i++) // getLastID is UID of all clients
{
if( TCP.isClientConnected(i) ) { // check and see if it's still around
// maybe the client is sending something
string str = TCP.receive(i);
TCP.send(i, "You sent: "+str);
}
}
There are two send() methods for ASCII string data that both get a message delimiter attached to them (by default [/TCP]):
send(int clientID, string message) - to send to a specific client sendToAll(string message) - to send to all clients
and there are two methods for sending raw ASCII data, i.e. without the message delimiter attached to them:
sendRawMsg(int clientID, const char * rawMsg, const int numBytes) - to send to a specific client sendRawMsgToAll(const char * rawMsg, const int numBytes) - to send to a specific client
And finally two methods for sending raw non-ASCII data like bitmaps, sounds, or other binary data formats:
sendRawBytes(int clientID, const char * rawBytes, const int numBytes) - to send to a specific client sendRawBytesToAll(const char * rawBytes, const int numBytes) - to send to a specific client
There are a few things to note:
- TCP is connection based which means it can be slower for things like video streams or lots of blobs of data, but more reliable than UDP. A not too technical description of the differences
- TCP is not HTTP. You'll probably find that tools like browsers send a lot more information that you're initially expecting, but you can easily pull out the parts of their request that you might need from the strings.
- Just because your ofxTCPServer is up and running does not mean that those ports on your computer will be open or that your IP will be visible. That's all configuration work to be done before your server can talk to the outside world.
The ofxTCPServer is threaded by default,
close( )
bool close()Close the server down and disconnect all clients.
disconnectAllClients( )
bool disconnectAllClients()disconnectClient( ... )
bool disconnectClient(int clientID)Disconnect a particular client.
getClient( ... )
ofxTCPClient & getClient(int clientID)getClientIP( ... )
string getClientIP(int clientID)Returns the ID that the client is connected from. This is useful for tracking clients that connect and disconnect.
getClientPort( ... )
int getClientPort(int clientID)Gets the port that the client is currently connected on.
getLastID( )
int getLastID()Returns the last UID assigned to a client. As this counts upwards, it's the best way to loop through all clients:
for(int i = 0; i < TCP.getLastID(); i++) // getLastID is UID of all clients
{
if( TCP.isClientConnected(i) ) { // check and see if it's still around
// maybe the client is sending something
string str = TCP.receive(i);
TCP.send(i, "You sent: "+str);
}
}
getNumClients( )
int getNumClients()Returns the number of connected clients, helpful for monitoring loads on a server but not for sending messages.
getNumReceivedBytes( ... )
int getNumReceivedBytes(int clientID)Returns the total bytes sent by a client.
getPort( )
int getPort()Returns the port that the server is currently serving on.
isClientConnected( ... )
bool isClientConnected(int clientID)Returns whether a client ID correlates to a connected client:
for(int i = 0; i < TCP.getLastID(); i++) // getLastID is UID of all clients
{
if( TCP.isClientConnected(i) ) { // check and see if it's still around
// maybe the client is sending something
string str = TCP.receive(i);
TCP.send(i, "You sent: "+str);
}
}
isClientSetup( ... )
bool isClientSetup(int clientID)isConnected( )
bool isConnected()ofxTCPServer( ... )
ofxTCPServer(const ofxTCPServer &mom)ofxTCPServer( )
ofxTCPServer()Constructor. You need to call setup() before your server itself is ready to receive connections.
operator=( ... )
ofxTCPServer & operator=(const ofxTCPServer &mom)peekReceiveRawBytes( ... )
int peekReceiveRawBytes(int clientID, char *receiveBytes, int numBytes)receive( ... )
string receive(int clientID)Receives raw bytes, such as an bitmap or audio data from a client indicated with the clientID:
for ( int i = 0; i < server.getLastID(); i++ ) {
if(server.isClientConnected(i)) {
string received = server.receive(clientID); // will end with delimiter, so make sure client is sending it
}
}
receiveRawBytes( ... )
int receiveRawBytes(int clientID, char *receiveBytes, int numBytes)Receives raw bytes, such as an bitmap or audio data from a client indicated with the clientID:
for ( int i = 0; i < server.getLastID(); i++ ) {
if(server.isClientConnected(i)) {
int received = server.receiveRawBytes(clientID, *receiveBytes, numBytes);
}
}
receiveRawMsg( ... )
int receiveRawMsg(int clientID, char *receiveBytes, int numBytes)send( ... )
bool send(int clientID, string message)Sends a string delimited with the delimiter value to a selected client.
sendRawBytes( ... )
bool sendRawBytes(int clientID, const char *rawBytes, const int numBytes)Sends raw bytes to a selected client. See sendRawBytesToAll()
sendRawBytesToAll( ... )
bool sendRawBytesToAll(const char *rawBytes, const int numBytes)Sends raw bytes to all connected clients, handy for sending an image or other binary data to a client. For instance:
ofImage img;
img.loadImage("tmp.jpg");
int imageBytesToSend = 7800;
int totalBytesSent = 0;
int messageSize = 256;
while( imageBytesToSend
sendRawMsg( ... )
bool sendRawMsg(int clientID, const char *rawMsg, const int numBytes)sendRawMsgToAll( ... )
bool sendRawMsgToAll(const char *rawMsg, const int numBytes)sendToAll( ... )
bool sendToAll(string message)Sends a string delimited with the delimiter value to all connected clients.
setMessageDelimiter( ... )
void setMessageDelimiter(string delim)This sets the message delimiter that your server will use when sending and receiving messages from clients. By default it's [\TCP] though you can have it be any value as long as it's consistent on both the client and server sides.
setVerbose( ... )
void setVerbose(bool _verbose)setup( ... )
bool setup(const ofxTCPSettings &settings)setup( ... )
bool setup(int _port, bool blocking=false)The port is the port that your server will serve up data on. It shouldn't be a commonly used port like 22 or 80, go with a higher number less likely to be used. The blocking parameter signals whether the client connections will be allowed to block on the servers thread as they send a message. This becomes important when you're dealing with very large messages coming from clients.
threadedFunction( )
void threadedFunction()waitConnectedClient( )
void waitConnectedClient()waitConnectedClient( ... )
void waitConnectedClient(int ms)~ofxTCPServer( )
~ofxTCPServer()