Boost logo

Boost Users :

Subject: [Boost-users] UDP bind problems
From: ustulation (ustulation_at_[hidden])
Date: 2014-01-13 05:10:43


{Windows 7, MinGW 4.8, boost 1.55}

I'm having some problems with UDP binds. I've a client that broadcasts
datagrams for listeners listening on specific port and binds to a port
itself if the listeners want to communicate something back.
The port on which the client needs to bind is X and the servers are
listening on Y.

Problem:
If I simulate a client-crash (eg., by causing segmentation fault by
dereferencing a nullptr) after binding the UDP socket to the port, then once
the client application is no longer running (no longer listed in Windows
Task Manager) netstat -ano | find "X" still shows that someone is bound to
port X and ip address of 0.0.0.0 (the client had specified the IP address as
any address). The PID cannot be found in Windows Task Manager. However when
I downloaded application TCPView I can see that a *<non-existent>* process
is still bound to 50000. On starting the client (without making it crash
this time) subsequently I get two behaviors:
<1> On some machines the client is unable to bind to the socket again
(although reuse_address option is set to true) and the error message is: An
attempt was made to access a socket in a way forbidden by its access
permissions.
<2> On other machines the client binds successfully but the read handler is
not called and the client does not receive any datagram on port X although
the servers are unicasting to the client port X. Infact <2> is true even for
launching multiple instances of the client on the same machine even if none
of the clients were deliberately made to crash and exist as zombie
processes. Only the 1st one gets datagrams.

Here is how client socket is set up:

if(!m_udpSocket.is_open())
   {
      m_udpSocket.open(m_localEndpoint.protocol(), errorCode);
//m_localEndpoint is address 0.0.0.0 and port X
      if(errorCode)
      {
         std::cerr << "Unable to open socket: " << errorCode.message() <<
std::endl;
      }
      else
      {
        
m_udpSocket.set_option(boost::asio::socket_base::reuse_address(true),
errorCode);
         if(errorCode)
         {
            std::cerr << "Reuse address option set failure. " <<
errorCode.message() << std::endl;
         }

         m_udpSocket.set_option(boost::asio::socket_base::broadcast(true),
errorCode);
         if(errorCode)
         {
            std::cerr << "Socket cannot send broadcast. " <<
errorCode.message() << std::endl;
         }
         else
         {
            m_udpSocket.bind(m_localEndpoint, errorCode);
            if(errorCode)
            {
               std::cerr << "Socket cannot bind...!! " <<
errorCode.message() << std::endl;
            }
         }
      }
   }

Can you explain why do I get <1> and <2> and what can I do to avoid them and
make socket bind even if there is some other process bound to that socket? I
need to support Windows, Linux and MAC

--
View this message in context: http://boost.2283326.n4.nabble.com/UDP-bind-problems-tp4657731.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net