class methods
- ofxTCPServer()
- ~ofxTCPServer()
- close()
- disconnectAllClients()
- disconnectClient()
- getClientIP()
- getClientPort()
- getLastID()
- getNumClients()
- getNumReceivedBytes()
- getPort()
- isClientConnected()
- isConnected()
- operator=()
- peekReceiveRawBytes()
- receive()
- receiveRawBytes()
- receiveRawMsg()
- send()
- sendRawBytes()
- sendRawBytesToAll()
- sendRawMsg()
- sendRawMsgToAll()
- sendToAll()
- setMessageDelimiter()
- setVerbose()
- setup()
- waitConnectedClient()
variables
Extends
This class extends others, you can call their methods on an instance of ofxTCPServer too:
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: 1) 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 2) 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. 3) 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,
ofxTCPServer()
ofxTCPServer::ofxTCPServer()
Constructor. You need to call setup() before your server itself is ready to receive connections.
disconnectClient(...)
bool ofxTCPServer::disconnectClient(int clientID)
Disconnect a particular client.
getClientIP(...)
string ofxTCPServer::getClientIP(int clientID)
Returns the ID that the client is connected from. This is useful for tracking clients that connect and disconnect.
getClientPort(...)
int ofxTCPServer::getClientPort(int clientID)
Gets the port that the client is currently connected on.
getLastID()
int ofxTCPServer::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 ofxTCPServer::getNumClients()
Returns the number of connected clients, helpful for monitoring loads on a server but not for sending messages.
getNumReceivedBytes(...)
int ofxTCPServer::getNumReceivedBytes(int clientID)
Returns the total bytes sent by a client.
isClientConnected(...)
bool ofxTCPServer::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);
}
}
peekReceiveRawBytes(...)
int ofxTCPServer::peekReceiveRawBytes(int clientID, char *receiveBytes, int numBytes)
receive(...)
string ofxTCPServer::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 ofxTCPServer::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);
}
}
send(...)
bool ofxTCPServer::send(int clientID, string message)
Sends a string delimited with the delimiter value to a selected client.
sendRawBytes(...)
bool ofxTCPServer::sendRawBytes(int clientID, const char *rawBytes, const int numBytes)
Sends raw bytes to a selected client. See sendRawBytesToAll()
sendRawBytesToAll(...)
bool ofxTCPServer::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 > 1 )
{
if(imageBytesToSend > messageSize) {
TCP.sendRawBytesToAll((char*) &img.getPixels()[totalBytesSent], messageSize);
imageBytesToSend -= messageSize;
totalBytesSent += messageSize;
} else {
TCP.sendRawBytesToAll( (char*) &img.getPixels()[totalBytesSent], imageBytesToSend);
totalBytesSent += imageBytesToSend;
imageBytesToSend = 0;
}
}
On the ofxTCPClient side this would look like:
bool dataRecd = false;
unsigned char buffer[7800];
int recd = 7800;
int totalReceived = 0;
int messageSize = 256;
while(recd > 0) {
if(recd > messageSize) {
tcpClient.receiveRawBytes( (char*) &buffer[totalReceived], messageSize);
recd -= messageSize;
totalReceived += messageSize;
} else {
tcpClient.receiveRawBytes( (char*) &buffer[totalReceived], recd);
totalReceived += recd;
recd = 0;
dataRecd = true;
}
}
if(dataRecd) {
img.setFromPixels( &buffer[0], 50, 52, OF_IMAGE_COLOR);
}
This example is a bit silly because it assumes that you know the exact size of an image, but extending this with different messages to send sizes and types beforehand isn't too difficult.
sendToAll(...)
bool ofxTCPServer::sendToAll(string message)
Sends a string delimited with the delimiter value to all connected clients.
setMessageDelimiter(...)
void ofxTCPServer::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.
setup(...)
bool ofxTCPServer::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.
Last updated 土曜日, 17 8月 2024 20:45:43 UTC - 99bfb4fd7929e233b87b05758efc36f91505592e
If you have any doubt about the usage of this module you can ask in the forum.
If you want to contribute better documentation or start documenting this section you can do so here
If you find anything wrong with this docs you can report any error by opening an issue