Non-blocking connect()?

This is a discussion on Non-blocking connect()? within the Networking/Device Communication forums, part of the General Programming Boards category; Every time I call connect() on a host that isn't alive it takes like 30 seconds to complete... I want ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Non-blocking connect()?

    Every time I call connect() on a host that isn't alive it takes like 30 seconds to complete... I want to minimize this time by specifying my own "timeout", so say... I want a timeout of 5 seconds, if connect() cannot find the host in 5 seconds then the function returns. I'm looking on MSDN and all I can see it say is this:

    Code:
    With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return 
    SOCKET_ERROR, and WSAGetLastError will return WSAEWOULDBLOCK. In this case, there are three possible scenarios:
    
    * Use the select function to determine the completion of the connection request by checking to see if the socket is writeable.
    But the only problem is that MSDN doesn't mention HOW to make the socket or connect() nonblocking.

    So I'm just wondering how I can do this, I'm going to be using a function Check_Host() and I'm guessing I'll need a non-blocking connect(), then a call to select() with a timeout specified of 5 seconds or whatever I choose, and if select() errors after that 5 seconds the host is assumed not alive.

    Only problem is I have no idea how to do the whole non-blocking thing Thanks.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Your assumption is pretty much correct. Just call select() on the nonblocking socket to see if it is writable. If select() times out, then the socket still hasn't connected after your timeout time. You can then call closesocket() on that socket and assume that the connection failed. Keep in mind that on some slow hosts, it is entirely possible that a connection attempt could take longer than 5 seconds.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by bithub View Post
    Your assumption is pretty much correct. Just call select() on the nonblocking socket to see if it is writable. If select() times out, then the socket still hasn't connected after your timeout time. You can then call closesocket() on that socket and assume that the connection failed. Keep in mind that on some slow hosts, it is entirely possible that a connection attempt could take longer than 5 seconds.
    Thanks for the reply. Ok well that's good to know, only problem now is I'm still uncertain how I'm supposed to put my socket in nonblocking state

    Quote Originally Posted by pobri19
    But the only problem is that MSDN doesn't mention HOW to make the socket or connect() nonblocking.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Code:
    fcntl(sockfd, F_SETFL, O_NONBLOCK);

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by bithub View Post
    Code:
    fcntl(sockfd, F_SETFL, O_NONBLOCK);
    Is that not a linux thing? I don't see fcntl() mentioned on MSDN. When I google for it, the examples say include "fcntl.h", the compiler seems to acknowledge the file is there (because it doesn't say file not found) but it still says

    Code:
    'fcntl': identifier not found
    'F_SETFL' : undeclared identifier
    'O_NONBLOCK' : undeclared identifier
    When I try and compile if my program calls fcntl().
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Sorry, forgot you were using Windows. Use ioctlsocket() instead of fcntl().

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by bithub View Post
    Sorry, forgot you were using Windows. Use ioctlsocket() instead of fcntl().
    Thanks
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  8. #8
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Hmm this is my attempt but it doesn't appear to be working :/ Says that the host is down even if it isn't.

    Code:
    int Host_Alive(arguments * args) {
    	args->serv.sin_addr.S_un.S_addr = inet_addr(args->sz_address);
    	args->serv.sin_family = AF_INET;
    	args->serv.sin_port = htons(args->port);
    
    	args->sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     
    	unsigned long iMode = 0;
    	ioctlsocket(args->sockfd, FIONBIO, &iMode); // put socket in non-blocking state
     
    	fd_set socket_set;
    	timeval timer;
     
    	socket_set.fd_array[0] = args->sockfd;
    	socket_set.fd_count = 1;
     
    	timer.tv_sec = args->timeout;
     
    	int ret = select(0, &socket_set, &socket_set, &socket_set, &timer);
     
    	if (!ret || ret == SOCKET_ERROR) {
    		printf("[!] Host %s:%d not alive!\n", args->sz_address, args->port);
    		free(args);
    		closesocket(args->sockfd);
    		return 0;
    	}
     
    	else
    		printf("[!] Host up!\n");
     
    	free(args);
    	closesocket(args->sockfd);
     
    	return 1;
    }
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about printing out what ret is, so you know why it's giving the error.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    You are not calling connect(), so it's safe to say that select() is never going to tell you that the socket has connected...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to initialize a non blocking socket using only winsock library
    By *DEAD* in forum Networking/Device Communication
    Replies: 4
    Last Post: 01-18-2008, 07:03 AM
  2. connect() function, strange error
    By Mr_Miguel in forum C Programming
    Replies: 1
    Last Post: 12-12-2006, 06:51 PM
  3. connect timeout
    By X PaYnE X in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-14-2005, 10:30 PM
  4. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM
  5. Advanced connect four game
    By Ion Blade in forum C++ Programming
    Replies: 10
    Last Post: 07-28-2002, 08:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21