TCP delimiter problem

Hi All

I’m trying to send a packet of data over localhost using TCP. This will eventually between 2 PCs over ethernet. I can send a single value without a problem. As soon as I try to send multiple values (currently mouseX and mouseY) in a single string using a delimiter I run into a problem. It seems that only one of these 2 values is received by the server per frame. So rather that getting X and Y every frame, the server receives X one frame and Y the next. This causes a delay of x2.

I tried the networkTCP examples that come with OF and I get the same results Every time ENTER is pressed and a delimiter is added the amount of delay on the server is increased by a multiple of one.

This is my first experience using TCP and I’m pretty new to networking so perhaps this is how it is supposed to work. If so, what is the best way to send multiple values in a single packet?

Thanks in advance.

James

I think I’m misunderstanding, but it almost sounds like you’re using the delimiter to separate the X and Y values? If you’re just sending multiple values in a single message with a character to separate values, i.e. “X=202:Y=281”, which turns into “[TCP]X=202:Y=281[/TCP]”, then that seems like something is wrong. If that’s the case, maybe you could post a little bit of code?

the problem with tcp is that is not message oriented but stream oriented. you can send a message with something but when you read a package in the other side it doesn’t necesarily has to have the complete message. That’s why you use delimiters, each time you read keep reading till you get the delimiter.

That’s taken care of by OF if you use send() and receive() by adding [/TCP] in send and reading till it gets that delimiter in receive so unless you are using sendRaw or receiveRaw, i don’t know what can be going wrong. can you post some code?

Thanks for getting back. I’m sending a message formatted like this “mouseX=123**mouseY=456”

Here’s the code in the update method in the client application:

  
  
if(isConnected)  
{  
    sentMsg = "mouseX=" + ofToString(ofGetMouseX()) + "**mouseY=" + ofToString(ofGetMouseY());  
    if(tcpClient.send(sentMsg))       
    {  
	//if data has been sent lets update our text  
	string str = tcpClient.receive();  
	if( str.length() > 0 )  
        {  
	    receivedMsg = str;  
	}  
    }  
}  
  

And this is in the server application:

  
  
for(int i = 0; i < tcpServer.getLastID(); i++)  
{  
        if(!tcpServer.isClientConnected(i))continue;  
          
	//calculate where to draw the text  
	int xPos = 15;  
	int yPos = 100 + (12 * i * 4);  
          
	//get the ip and port of the client  
	string port = ofToString(tcpServer.getClientPort(i));  
	string ip   = tcpServer.getClientIP(i);  
	string info = "client "+ofToString(i)+" -connected from "+ip+" \non port: "+port;  
         
	//we only want to update the text we have recieved there is data  
	string str = "from client - "+tcpServer.receive(0);  
          
        printf("frame = %i, str = %s \n", ofGetFrameNum(), str.c_str());  
          
        //draw the info text and the received text bellow it  
	ofDrawBitmapString(info, xPos, yPos);  
	ofDrawBitmapString(str, xPos, yPos + 50 + (i*50));  
 }  
  

To illustrate my issue further, the printf function in the server code produces the following results:

  
  
frame = 988, str = from client - mouseX=26   
frame = 989, str = from client - mouseY=32   
frame = 990, str = from client - mouseX=51   
frame = 991, str = from client - mouseY=62   
frame = 992, str = from client - mouseX=96   
frame = 993, str = from client - mouseY=116  
  

As you can see, the application is only allowing one value to be received per frame, which is causing a queue of data somewhere.

@Arturo, if I understand you correctly, the delimiter is used purely to end the message, not to separate the values in a single packet. Is this correct? If so I need to write a function to split the string into an array.

I’ve attached a couple of stripped down projects.

TCP_Test.zip

so the problem is you are using:

  
tcpServer.setMessageDelimiter("**");  

which is making the addon treat ** as the message delimiter so it cuts each message whenever it gets a **

@Arturo, if I understand you correctly, the delimiter is used purely to end the message, not to separate the values in a single packet. Is this correct? If so I need to write a function to split the string into an array.

yes the message delimiter is only to be used internally so a protocol like tcp which is not message oriented can be used to send and receive complete messages.

remove the setDelimiter call and you’ll get the whole message at once, then use ofSplitString(msg,"**") to divide it in x and y

Thanks for clearing that up Arturo.