ofxTCPClient::receiveRaw() bugs

ofxTCPClient’s receiveRaw() call seems to have two bugs:

  1. It calls TCPManager.Receive, which calls recv. If it’s nonblocking, recv returns -1 and errno = EAGAIN when there’s no data available, but receiveRaw doesn’t account for this, or for other errors.

  2. The function returns a character buffer and has the system implicitly convert it to a string, but there’s no terminating null to determine the string length.

An improved function call might look like this:

  
#ifdef TARGET_WIN32  
    #define TCP_NODATA WSAEWOULDBLOCK  
#else  
    #define TCP_NODATA EAGAIN  
#endif  
  
string ofxTCPClient::receiveRaw() {  
    messageSize = TCPClient.Receive(tmpBuff, TCP_MAX_MSG_SIZE);  
    if(messageSize==0) {  
        close();  
    }  
    else if (messageSize == -1) {  
        // no data if nonblocking, or error  
        messageSize = 0;  
        // perhaps we should check for other nonfatal errors here.  
        if (errno != TCP_NODATA)  
            close();  
    }  
    // the buffer size is TCP_MAX_MSG_SIZE + 1,  
    // so this should never overflow  
    tmpBuff[messageSize] = 0;  
    return tmpBuff;  
}  

I’d think something similar should work for receiveRawBytes().

Thanks,

Danny Rosenberg
META/DMA

I just noticed that we’re having some problems with the MPE library (http://code.google.com/p/mostpixelsever/) related to this. Messages come in multiple times, etc. I rewrote the receiveRaw() method to look like:

  
//--------------------------  
string ofxTCPClient::receiveRaw(){  
	messageSize = TCPClient.Receive(tmpBuff, TCP_MAX_MSG_SIZE);  
	  
	if (messageSize > 0 && messageSize < TCP_MAX_MSG_SIZE) {  
		tmpBuff[messageSize] = '\0';  
	} else if (messageSize==0){  
		close();  
	} else if (messageSize == -1) {  
		tmpBuff[0] = '\0'; // tmpBuff = "";  
	}  
	return tmpBuff;  
}  

But yours is probably better!

Hi, I have met problems, can you give me some advice? thanks.

the server:
WallServer: framerate = 30, screens = 2, debug = true
Starting server: dell-1f9ae5b866/192.168.0.100 9002
/127.0.0.1:18745 connected.
S0
Connecting Client 0
Connection: adding message to next frameEvent: T305,303
Connection: adding message to next frameEvent: T287,234
Connection: adding message to next frameEvent: T287,234
Connection: adding message to next frameEvent: T287,234
Someone left? java.net.SocketException: Connection reset
Removing Connection 0
MPEServer: resetting frame count.

the client:
OF_ERROR:ofxNetwork:d:\program\of_prerelease_v0062_vs2008_fat\addons\ofxnetwork\src\ofxtcpmanager.cpp:304 unknown error:0 see errno.h for description of the error