Socket connect() time out

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Socket connect() time out

Postby Red Squirrel » Sat Nov 26, 2005 5:23 pm

What can I do to this code to make it so it only waits 5 seconds to connect? If it can't connect after 5 seconds, I want it to fail, instead of staying stuck there for like 15 minutes. Not sure who came up with making it this complicated, since this is something silly, that I've been trying to figure out for months.

Code: Select all
//----------socket handling
        int sockfd, numbytes; 
        struct hostent *he;
        struct sockaddr_in their_addr; // connector's address information

       he=gethostbyname(host);           
       
        if (he== NULL) {  // get the host info
            //cout<<"Error resolving host"<<endl;
            return -3;
        }


        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd == -1) {
            //cout<<"Error creating socket"<<endl;
            return -2;
        }
       
             
       
        their_addr.sin_family = AF_INET;    // host byte order
        their_addr.sin_port = htons(port);  // short, network byte order
        their_addr.sin_addr = *((struct in_addr *)he->h_addr);
        memset(&(their_addr.sin_zero), '\0', 8);  // zero the rest of the struct
       
       
    //cout<<endl<<endl<<"connecting"<<endl;
        if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
             //cout<<"Error connecting"<<endl;
            return -1;
        }     
       
close(sockfd);

//-----------end socket handling



I've heard of setsockopt, I just can't figure out how to properly use it, none of the tutorials I find show any good examples.
Red Squirrel
 
Posts: 75
Joined: Thu Jan 01, 2004 7:31 pm

Postby Alvaro » Sun Nov 27, 2005 7:15 pm

You can make the socket non-blocking and then use select() to control all your socket handling. You can specify a timeout in your call to select(). I haven't done this type of thing in years, so I don't remember all the little details, but that's the general idea.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby Seronis » Fri Dec 23, 2005 10:34 pm

after your call to socket() and prior to connect() do this...

Code: Select all
#if defined(WIN32)
      u_long FAR cmd = 1;
      if( ioctlsocket( sockfd , FIONBIO, &cmd ) != 0 )
#else
      if( fcntl( sockfd , F_SETFL, O_NDELAY ) == -1 )
#endif
      {
         std::cout << "Error setting non-blocking mode. die !" << std::endl;
      }


that will set non blocking mode on the descriptor so that if the connect() doesnt immediately succeed it will return with SOCKET_ERROR and errno / WSAGetLastError() will be EWOULDBLOCK / WSAEWOULDBLOCK respectively.

Now after that it will be up to YOU to do is to set up some type of timer so that the connect call will get repeated a couple times a second till whatever duration you deem appropriate. Good approach to the user would be upon the First failed connect update a status message that the connection failed and you are retrying.
Seronis
 
Posts: 2
Joined: Fri Dec 23, 2005 10:26 pm
Location: Ohio


Return to Unix/Linux

Who is online

Users browsing this forum: No registered users and 1 guest