Thread: UDP Socket hangs when not available

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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, 04: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, 11: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, 11:59 AM