7

I have this code segment that is designed to connect to a server using a socket connection. However if it can not connect to the server within a certain amount of time I would like it to stop trying. I tried to do this with this nonblocking socket and the select command but select is always returning 1 indicating that the server exists when nothing exists at the address I give it. Any Ideas?

SOCKET tcp_client( char *hname, char *sname )  {    
    fd_set fdset;
    struct sockaddr_in peer;
    SOCKET s;
    FD_ZERO(&fdset);
    // FD_SET(STDIN, &fdset);
    FD_SET(s, &fdset);
    errno=1;
    struct timeval tv;
    tv.tv_sec = 15;
    set_address( hname, sname, &peer, "tcp" );
    s = socket( AF_INET, SOCK_STREAM, 0 );

    int n = 1;
    fcntl(s, F_SETFL, O_NONBLOCK);

    if ( !isvalidsock( s ) )
    {
        printf("Socket Call Failed: %s\n", strerror(errno));
        return(0);
    }

    int x = 0;

    int status = connect( s, ( struct sockaddr * )&peer, sizeof( peer ) );

    if(status < 0) {
        printf("Status: %i\n", status); 
    }

    int retVal = select(s+1, &fdset, NULL, NULL, &tv);
    printf("retVal: %i\n", retVal);

    if (retVal == 1) {
        int so_error;
        socklen_t slen = sizeof so_error;
        getsockopt(s, SOL_SOCKET, SO_ERROR, &so_error, &slen);
        if (so_error == 0) {
            printf("work\n");

            x =1;
        } else {
            printf("fail\n");
            x = 0;
        }
    } else {     
        printf("noSocks\n"); 
    }


    if (x ==0 )
    {
        printf("Connect Failed: %s\n", strerror(errno));
        L("libOnexc: Connect to socket failed");
        close(s);
        return(0);
    } 

    return s;
}
Chenmunka
  • 685
  • 4
  • 21
  • 25
Daniel
  • 541
  • 2
  • 5
  • 16

2 Answers2

13

One problem I see is that you stick s in the fdset before you have created the socket. You need to do the

FD_SET(s, &fdset);

after you've created the socket because s is just an integer and so will not be the right value until after the call to socket().

EDIT

Like this:

.
.
.
SOCKET s;
errno=1;
struct timeval tv;
tv.tv_sec = 15;
set_address( hname, sname, &peer, "tcp" );
s = socket( AF_INET, SOCK_STREAM, 0 );

int n = 1;
fcntl(s, F_SETFL, O_NONBLOCK);

if ( !isvalidsock( s ) )
{
    printf("Socket Call Failed: %s\n", strerror(errno));
    return(0);
}    

FD_ZERO(&fdset);
FD_SET(s, &fdset); // don't put socket in set until it is actually created 
Brian Rothstein
  • 1,312
  • 10
  • 20
  • I moved it to after the socket is created but now select always returns 0 even when trying to connect to a machine I know exists... – Daniel Apr 30 '11 at 18:45
  • `fcntl(s, F_SETFL, O_NONBLOCK);` is incorrect. You overwrite other flags that may be set. Also see [How do I change a TCP socket to be non-blocking?](https://stackoverflow.com/q/1543466/608639) – jww Mar 03 '19 at 05:46
13

You need to check whether the socket is ready for writing (the second fd_set * argument to select), not reading (the first one).

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711