Thread: Setting timeout time...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Question Setting timeout time...

    How to can I set the connect()ion timeout time?


    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    yeah, I've read it. But the problem that
    Code:
    fcntl(sock, F_SETFL, O_NONBLOCK);
    is not supported by winsock.

    Isn't there any other way to set the timeout time?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Exclamation

    connect() have a timeout
    embedded into them. The theory behind this is that only the stack has
    all the information necessary to set a proper timeout.

    connect()'s timeout value can't be changed... I guess that leaves me with select() and the non blocking option, that I can't even find at the msdn.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    EDIT: Cleaned up code for prosperity. Error checking is still non-existant.

    Code:
    int main(void)
    {
    	SOCKET  s;
    	HANDLE  hEvent;
    	ULONG   setting;
    	WSADATA wd;
    	int     cbRecv;
    	char    buf[1024];
    	struct  sockaddr_in sa = { 0 };
    	WSANETWORKEVENTS ne;
    
    	WSAStartup(MAKEWORD (2,2), &wd);
    
    	hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    
    	s = socket(AF_INET, SOCK_STREAM, 0);
    
    	/* Puts socket in non-blocking mode and 
    	 * tells windows to signal hEvent when the 
    	 * connect() has completed or an error occurs. */
    	WSAEventSelect(s, hEvent, FD_CONNECT); 
    
    	sa.sin_family		= AF_INET;
    	sa.sin_addr.s_addr	= inet_addr("66.35.250.203");
    	sa.sin_port		= htons(80);
    
    	connect(s, (struct sockaddr *) &sa, sizeof(struct sockaddr_in));
    
    	/* Wait 3 seconds for socket to connect() or error to occur. */
    	if (WaitForSingleObject(hEvent, 3000) == WAIT_OBJECT_0)
    	{
    		WSAEnumNetworkEvents(s, NULL, &ne);
    
    		if (ne.iErrorCode[FD_CONNECT_BIT] == 0)
    			printf("Connected successfully.\n");
    		else
    			printf("Failed to connect with error %d.\n", ne.iErrorCode[FD_CONNECT_BIT]);
    	}
    	else
    	{
    		printf("Timed out.\n");
    	}
    
    	/* Disable event signalling */
    	WSAEventSelect(s, hEvent, 0);
    	CloseHandle(hEvent);
    
    	/* Put socket back into blocking mode */
    	setting = 0;
    	ioctlsocket(s, FIONBIO, &setting);
    
    	send(s, "GET / HTTP/1.0\r\n\r\n", sizeof("GET / HTTP/1.0\r\n\r\n") - 1, 0);
    	cbRecv = recv(s, buf, sizeof(buf) - 1, 0);
    	closesocket(s);
    
    	if (cbRecv != SOCKET_ERROR)
    	{
    		buf[cbRecv] = '\0';
    		printf("%s", buf);
    	}
    	else
    	{
    		printf("Error %d on recv.\n", WSAGetLastError());
    	}
    
    	getchar();
    	return 0;
    }
    Last edited by anonytmouse; 04-06-2004 at 08:39 PM.

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    But I thought Hammer said that it will always allow me to write, even when the connection wasn't established yet.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Yes I didn't think of that. My code would need a call to WSAEnumNetworkEvents() to check for errors after the WaitForSingleObject(). I'd suggest you use the code in the thread that hammer posted, as it is simpler anyway. Just replace ioctl() with ioctlsocket() and errno with WSAGetLastError().

    EDIT: Replaced code in previous post with working version.
    Last edited by anonytmouse; 04-06-2004 at 08:41 PM.

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    thank you very much.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Weird Times I'm getting
    By afflictedd2 in forum Linux Programming
    Replies: 8
    Last Post: 07-23-2008, 07:18 AM
  3. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  4. What is the best way to record a process execution time?
    By hanash in forum Linux Programming
    Replies: 7
    Last Post: 03-15-2006, 07:17 AM
  5. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM