Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
20693 Discussions

Can the connect() for a TCP client be configured as "blocking" ?

Altera_Forum
Honored Contributor II
1,191 Views

I'm developing a TCP client using NicheStack. 

Can the connect() for a TCP client be configured as "blocking" ? 

 

Currently, if no server is listening for the connection the connect() returns -1. 

So, I have to put the connect() in a loop with a 500 millisecond delay between connection attempts.
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
493 Views

It's done when you create the socket and I believe it is blocking by default...in most network stacks. Double-check your socket settings.

0 Kudos
Altera_Forum
Honored Contributor II
493 Views

Yes, the socket is blocking by default. 

In order to set non-blocking mode you must call: 

setsockopt(your_socket, SOL_SOCKET, SO_NONBLOCK, &max_socket, sizeof(int)); 

IIRC you could also set the default behaviour in Nichestack configuration file ipport.h
0 Kudos
Altera_Forum
Honored Contributor II
493 Views

Thanks for confirming that the connect() should block by default. For some unknown reason, in my case, it does not block. 

I have tried to use setsockopt() to guarantee that the socket is setup as "blocking" but the connect() stills returns -1  

if a server is not currently listening.  

 

By the way, my recv() call does block as expected. I'm going to continue to investigate 

why the connect() is not behaving as expected. I'm also going to check out the file "ipport.h".
0 Kudos
Altera_Forum
Honored Contributor II
493 Views

After doing further investigation (using the debugger), the error causing the connect() to return -1 is ECONNREFUSED. 

Of course, since there's no server listening for this client that would make sense but since the connect() should 

be blocked until the server is listening, why is the error ECONNREFUSED ?
0 Kudos
Reply