Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
11 years ago

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

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.

4 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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".
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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 ?