Network samples changes.

Hi Guys,

I managed to learn openframeworks and c++ quit fast thanks to great examples you guys provide.
It’s more fun to learn by examples than reading dozens of articles/books.

But somethings wasn’t clear to my.
People use TCP to be sure the packets arrive at their the destination.
TCP has builtin error checking and retransmitting.

The TCP sender sample sends the same message every update loop.
Wouldn’t be better to add typed at line 44 in testApp.ccp and add a typed=false at the end

  
  
	if(weConnected){  
		if(typed)  
		{  
			tcpClient.send(msgTx);  
			cout << "send" << endl;  
			//if data has been sent lets update our text  
			string str = tcpClient.receive();  
			if( str.length() > 0 ){  
				msgRx = str;  
			  
			}  
			typed = false;  
		}  
  

I don’t see the point of sending the same message every update loop ?

The udp sample does the oposite then the TCP sample
It sends data once the mouse is released, while UDP is made to send a lot of data really fast.

I would change the upd sender.

  
  
void testApp::mouseDragged(int x, int y, int button){  
	stroke.push_back(ofPoint(x,y));  
  
		string message="";  
	//for(int i=0; i<stroke.size(); i++){  
		message=ofToString(x)+"|"+ofToString(y)+"[/p]";  
	//}  
	int sent = udpConnection.Send(message.c_str(),message.length());  
}  
  
void testApp::mouseReleased(int x, int y, int button){  
	//string message="";  
	//for(int i=0; i<stroke.size(); i++){  
	//	message+=ofToString(stroke[i].x)+"|"+ofToString(stroke[i].y)+"[/p]";  
	//}  
	//int sent = udpConnection.Send(message.c_str(),message.length());  
}  
  

Then remove the stroke.clear() at the udp receiver

  
  
void testApp::update(){  
  
	char udpMessage[100000];  
	udpConnection.Receive(udpMessage,100000);  
	string message=udpMessage;  
	if(message!=""){  
		//stroke.clear();  
		float x,y;  
		vector<string> strPoints = ofSplitString(message,"[/p]");  
		for(int i=0;i<strPoints.size();i++){  
			vector<string> point = ofSplitString(strPoints[i],"|");  
			x=atof(point[0].c_str());  
			y=atof(point[1].c_str());  
			point.clear();  
			stroke.push_back(ofPoint(x,y));  
		}  
	}  
}  
  

I could be wrong but I think these little changes make better use of the protocols

zip file with VS projects
http://www.lab101.be/networkSamples.zip

well, i’ll say that tcp is reliable and connection oriented and udp is not. there’s nothing that tells that tcp is for small messages or udp for long ones or viceversa. for example tcp is used in ftp (super long chunks of data) and udp in dns queries (really small messages).

it’s true that the tcp send method seems more like an udp one, and indeed is tricked to send a final terminator to behave like that but most people using ofxNetwork just want to communicate short messages with other apps so it’s really useful like that.

also the way the example sends the message every update is a way of showing how tcp controls the speed between two hosts for example when the buffer is full. try writing a long message to see how the sending periodically stops after sometime and resumes. that’s tcp controlling the speed of the sender when the receiver cannot get more data.

udp sends little messages compared to tcp, then it’s used for things like video streaming cause it doesn’t introduce so much overhead as tcp, you can use broadcast and multicast and sometimes you don’t need the tcp reliability but even with that you need to add some kind of control for the order of the messages for example.

so sending point by point in the udp example can lead for example to unordered points in the destination.

unless you need very high optimization, i would suggest tcp for reliable communication no matter the quantity of data you are sending and udp for sending messages where the order of arrival or loosing some doesn’t matter.

that said, the examples can really show better the difference between the two protocols that they do now.

Hey Arturo,

Thx for clearing this out !
I totally forgot about udp not ordering the packages.

The purpose of the continuous sending at the update loop is clear to me know.
Maybe it’s better to add the reason for this in the comment of the sample for other users ?

Because it wasn’t clear for me I made the mistake of using the TCP sample code for my project which caused a delay when by sending a lot of data in the update loop.
When I changed it to UDP it was super fast.

Project video: http://vimeo.com/10373446
When using TCP the movement of the letters in the top screen had delays from 1s to 5 sec depending on the sentence size and update frequency.
With UDP the delay is non existing.

If someday I come up with a better UDP sample is it possible to commit it to OF ?

When using TCP the movement of the letters in the top screen had delays from 1s to 5 sec depending on the sentence size and update frequency.

that was sending everything, every update loop, right? if not seconds seems to much delay just for tcp/udp overhead difference. didn’t realize about that but perhaps it can be good idea to not send everytime in the tcp example to avoid people copying the code having problems like that.

yes, if you have some change or better idea for any example, post it in the forums or you can use github and do your changes there.

that was sending everything, every update loop, right?

When mouseDragged event which causes a lot of events when dragging so maybe it’s comparable to the update speed.