UDP Socket hangs when not available

This is a discussion on UDP Socket hangs when not available within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, I'm making a sort of 'query' tool for game servers, to get some information etc. It works pretty well, ...

  1. #1
    C++ Programmer
    Join Date
    Aug 2005
    Posts
    39

    UDP Socket hangs when not available

    Hi,

    I'm making a sort of 'query' tool for game servers, to get some information etc. It works pretty well, except when you fill in an hostname which doesn't exists, it keeps hanging at the recvfrom() call. My GUI blocks and than I must kill the app.

    UDP Sockets are unconnected, so I can't find a way to check if the given hostname is online. Does somebody knows it?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Perhaps use select() with a timeout to observe the socket in question before attempting a recv() on it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Programmer
    Join Date
    Aug 2005
    Posts
    39
    Yay, got it working now

    This is my function:
    Code:
    /**
     * Checks if the socket is ready for reading
     */
    bool Socket::can_read()
    {
    	int r= 0;
    	fd_set rset;
    	struct timeval tv;
      
    	FD_ZERO(&rset);
    	FD_SET(this -> socket, &rset);
    	tv.tv_sec= this -> timeout;
    	tv.tv_usec= 0;
      
    	do 
    	{
    		r = select(this -> socket + 1, &rset, NULL, NULL, &tv);
    	}
    	while(r == -1 && errno == EINTR);
    
    	return (r > 0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. UDP socket partial reading
    By mynickmynick in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-25-2009, 11:43 AM
  2. Can I bind a UDP socket to a port, but send to any other port?
    By trillianjedi in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-25-2009, 03:27 PM
  3. Error in creating socket in a client (UDP)
    By ferenczi in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-27-2008, 10:11 AM
  4. read/write through udp socket
    By xErath in forum Networking/Device Communication
    Replies: 3
    Last Post: 05-22-2005, 05:43 PM
  5. problem with UDP WSAAsyncSelect socket, no callback message received???
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-29-2004, 10:59 AM

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