TCP client connection timeout

Hi, I’m having some trouble with ofxTCPClient blocking my program while trying to connect to a server. I’m running a client.setup(string ip, int _port) function every 5 seconds to reconnect to the server if it’s not connected. When the server is not running (which i’m purposely testing for), the app pauses/blocks for quite a while and eventually timesout (191 ETIMEDOUT).

I thought it was non blocking by default, or does this only apply to the sending/receiving of messages? Maybe I’ll have to thread it?


Can you check to see if your app is hanging at the call to getpeername() inside ofxTCPManager::GetRemoteAddr()? You might want to check out threading your TCP interface, but it could be a little bit tricky.

Hey Joshua, it hangs on this line in ofxTCPManager’s Connect method-

bool ret = (connect(m_hSocket, (sockaddr *)&addr_in, sizeof(sockaddr)) != SOCKET_ERROR);

Hangs for about a 70-90 seconds before throwing the timeout message. For testing the error, I just shut down the computer i’m trying to connect to.

I resorted to threading the client, and it works fine. I would of preferred not to but hey- if it works.


Hi Trent

How much did you thread? Only the setup or the whole communication?

I’m experiencing the same problem in IOS - app blocks and exits when the server is not connected to the network. Apparently the server software does not have to be running, but if no device is present at the specified ip it fails.


Hey Tom, yeah I threaded the whole tcp client. But you could probably just thread the setup/connecting part, because after it’s connected the client is already non blocking by default anyway.

Ok thanks, will try.

Is it a bug that it cannot handle no response or just my lack of error-handling?

There’s no error handling for that part- so maybe it is a bug. Another issue I had with ofxTCPManager, was not being able to reconnect to a socket that was already in use (every 2nd app launch it thought it was busy). Fix for this was here…;ofxnetwork-socket-address-already-in-usequot;/7284/0


i have the same issue here and i try to understand how to thread the connection. Do you have an example please ?
I think the best thing would be to thread the whole tcpClient connexion, but maybe just the connection can be ok as, like someone said, once it is connected everything is ok.

Can anyone provide some example of code for threading a tcp connection please ?

Thanks a lot

Hey SMarty,

I’ve attached a cutdown version of the code I used. It has an example of a threaded server and client.

Note: I’ve also replaced the Bind function in ofxTCPManager for socket reconnection problems, see link in previous post.

Good luck,


Thanks a lot, i will have a look at you code. Hope this helps.
Thanks for sharing anyway !

i had a look at your code.
you don’t seem to thread the whole connection process, do you ?
I’m working on a project with same goals : send messages to servers and read their answer.

I don’t manage servers, only client connections.

Is it better (easier ?) to thread only the connection trial (i.e. client.setup(host, port):wink: ?

I have multiple methods like sendCommand(), updateStatus() etc… i don’t clearly understand how to implement them. Do i need a thread for every method ? because if i understand well, only one thread is possible (threadedFunction()) ?

PS : for instance, i don’t connect and then send command. Everything is included in the method. for exemple :

string sendCommand (string command) {  
//try to connect  
//if connected, send "command"  
//read answer  
//return answer  
void updateStatus() {  
// try to connect  
// if connected, send query POWER  
// update power status value  
// send query ERROR  
// update error status  
etc ...  

I would appreciate any advice

Thank you

PS : And in all the examples i found concerning Threading, they write a threadedFunction() in a separate header file and call it with start() and stop() methods. I don’t see those in you code !

hey trent,

I have same problem. Could you attach your code again plz??