Problem connecting ofxTCPClient : need Threading help !


I am trying to connect to an IP address that doesn’t exist just to test connexion feedback. This test should stop the process and send error message like “unable to connect or bad IP” when the connexion fails, otherwise we can go on an send messages over the network.

If the address is ok, the connexion seems ok.

If the address is not ok the application freezes

I am using the ofxTCPClient library with oF 0.7.4 un MacOSX

Here is a sample of my code…

déclare :

ofxTCPClient netClient;  

Then :

if(!netClient.setup(IPAddress, PJLINK_PORT)){return "CONNECTION ERROR !";}  

How do you make a network connexion ?


i do a ping test to see if a specific IP exists.

like so:

  string pingStr = (string)"ping -c 1 -t 1 " + serverIP ;  
                int flag = system( pingStr.c_str());  
                if(flag == 0){  
                    bServerConnected = true;  
                    bServerConnected = false;  
                    //   cout<<ofGetTimestampString()<<endl;  
                    cout<<"cloud not connect to timing server at IP "<<serverIP<<endl;  

might be good to put this in its own thread, otherwise the rest of the app will freeze for a moment.


yes that’s a good idea ! Thanks for the tip i will try this.
But i was wondering why my solution was blocking as long as the method is supposed to return a status code.

anyone experienced such a problem ?

thanks again for the tip

Edit : I just realized that the app is freezing until connection timeout occurs. You talk about thread, this kind of application requires threads ? or the library has problem handling the connexion ? I have never used threads…


So after grabbing information over the net, it seems necessary to use threads to handle clean connexions…

Threading network connexion seems necessary but very few information and examples exists for that purpose !
I’ve studied the oF thread example but it is really basic and i can’t manage to adapt this exemple to my need.

is there any master to teach in here ? :wink:


what i have for now :

string ofxPJCtrl::sendQueryCommand(string command)  
		string msgRx;  
		        if(!pjClient.setup(IPAddress, PJLINK_PORT)) {return "CONNECTION ERROR !";}  
			cout << "connection established: " << IPAddress << ":" << PJLINK_PORT << endl;  
			while (msgRx.length() < 8)  
				msgRx = pjClient.receiveRaw();  
				if(msgRx.length() > 6)  
					cout<< msgRx << endl;  
		cout << "sending command: "<< command << endl;  
		msgRx = "";  
		while (msgRx.length() < 8)  
			msgRx = pjClient.receiveRaw();  
			cout<< msgRx << endl;  
		connected = false;  
		return msgRx;  

you can also take a look at ofxKinectStreamer addon:

and see how it’s done there


i am having a look at it but as much as i can understand there are no threads for the client.setup() process. Only send and receive commands are threaded. I don’t understand why.

	cout << "try to initialize receiver" << endl;  
	connected = client.setup(ip, port, true);  
	client.setVerbose(true); // remove when done testing  
	cout << "Receiver initialized" << endl;  

not really helping to answer your question… but in my experience it makes sense to offload all projector-management stuff to another script, e.g. python has pjlink-libs that are working very well. in exhibition context i usually have a python or node.js script that is the ‘master’ (downloading contents, switching stuff on / off, start / stop gui-apps etc). parsing protocols in cpp is just too messy for my taste.

btw, thanks for your ping-question & answers, that’s why i landed here initially :wink:


thanks for your comment.
Do you have examples of python scripts for that purpose ?
I don’t know python but was think giving it a try.

By the way, python seems ok under osx ?

hey SMarty,

have a look at

installing python libs/eggs might be out of scope of this forum, but i suggest you give it a try. python comes pre-installed on Macs. have fun! (and don’t get too used to python, 'cause c++ is a pain after that ;))

:wink: yeah i know, coming from the linux world i know python is really a really nice piece of software.
Problem is you can’t compile a standalone program ?

yes you can, several helper apps can do it. check out platypus

it can create standalone app-bundles fom python and other scripting languages…with lots of options and also works out of the box.

Nice !
Well… i’m gonna give python a look !
For this kind of project it seems really usefull. But for video or 3D i need specialized tool like oF, PD or Processing.

Thanks a lot