class methods
- ofxTCPClient()
- ~ofxTCPClient()
- close()
- getIP()
- getNumReceivedBytes()
- getPort()
- isConnected()
- peekReceiveRawBytes()
- receive()
- receiveRaw()
- receiveRawBytes()
- receiveRawMsg()
- send()
- sendRaw()
- sendRawBytes()
- sendRawMsg()
- setMessageDelimiter()
- setVerbose()
- setup()
- threadedFunction()
variables
The ofxTCPClient is what you used to connect to another server and request information. This other server could be a service running on your local machine, or it could somewhere out there in the internet. As of version 0.8 it doesn't do SSL or other tricky things well, nor does it handle forms or GET and POST requests but you can handle strings or raw bytes. A trivial usage might look like the following:
void ofApp::setup()
{
bool connected = tcpClient.setup("127.0.0.1", 11999);
}
void ofApp::update()
{
if(tcpClient.isConnected()) {
string str = tcpClient.receive(); // did anything come in
}
}
void ofApp::keyReleased(int key)
{
if(tcpClient.isConnected()) {
tcpClient.send("HELLO WORLD!");
}
}
To receive something meaningful from a regular webserver you'll want to at least do something like the following:
string msg = "GET /index.html HTTP/1.1\r\n";
tcpClient.send(msg);
but if you're using ofxTCPServer, then you're free to invent whatever sort of low level communication you'd like. Another important element of the ofxTCPClient and of TCP communication in general, is the message delimiter. You can see that using:
tcpClient.setMessageDelimiter("\n");
This is important because the message delimiter used by your client has to match what your sever is using. If the server uses '\r\n', then your client needs to do the same so that the server knows when a message has ended.
ofxTCPClient()
ofxTCPClient::ofxTCPClient()
Constructor. This doesn't prepare your client to send and receive information though, you need to call the setup() method for that.
getIP()
string ofxTCPClient::getIP()
Returns the IP address number set in the setup() method of the client.
getNumReceivedBytes()
int ofxTCPClient::getNumReceivedBytes()
Returns the number of bytes that a single call to receive has gotten.
getPort()
int ofxTCPClient::getPort()
Returns the port number set in the setup() method of the client.
isConnected()
bool ofxTCPClient::isConnected()
Returns whether the client is currently connected to a server.
receive()
string ofxTCPClient::receive()
Receives ASCII encoded data from the server. You should make sure you check that the client is connected before calling this.
if(tcpClient.isConnected()) {
string str = tcpClient.receive();
cout << str << endl;
}
receiveRaw()
string ofxTCPClient::receiveRaw()
This receives the raw bytes from a server. Like with receive(), you should check that the client is connected before calling this.
if(tcpClient.isConnected()) {
string s = tcpClient.receiveRaw();
cout << str << endl;
}
receiveRawBytes(...)
int ofxTCPClient::receiveRawBytes(char *receiveBytes, int numBytes)
This retrieves non-ASCII from a server, handy for receiving an image or other binary data to a client. For instance:
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);
}
On the ofxTCPServer side this would look like:
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;
}
}
send(...)
bool ofxTCPClient::send(string message)
Send the message, which can be as complex as a full GET request or as simple as raw string.
sendRaw(...)
bool ofxTCPClient::sendRaw(string message)
This sends the message as raw, i.e. not ASCII encoded. This is what you'll want to do if you're sending bitmap data or other kinds of non-text information.
sendRawBytes(...)
bool ofxTCPClient::sendRawBytes(const char *rawBytes, const int numBytes)
This method sends raw bytes (i.e. not ASCII encoded bytes) to the server.
setMessageDelimiter(...)
void ofxTCPClient::setMessageDelimiter(string delim)
This is important because the message delimiter used by your client has to match what your sever is using. If the server uses '[\TCP]', which is the default for ofxTCPClient and ofxTCPServer, then your client needs to do the same so that the server knows when a message has ended. ofxTCPServer allows you to use whatever delimiter you want.
setup(...)
bool ofxTCPClient::setup(string ip, int _port, bool blocking=false)
You call this to setup what IP and port your client will try to connect to:
tcpClient.setup("127.0.0.1", 11999);
It returns whether the connection has successfully been made. Don't mistake this for a URI or URL, it comes before that, where the connection is made so that files or data streams can be requested.
Last updated 火曜日, 19 11月 2024 17:23:45 UTC - 2537ee49f6d46d5fe98e408849448314fd1f180e
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