How to rebind another UDP socket port properly?

Posted by Jollian on Stack Overflow See other posts from Stack Overflow or by Jollian
Published on 2010-03-14T02:50:27Z Indexed on 2010/03/14 2:55 UTC
Read the original article Hit count: 286

Filed under:
|
|

When my client application launchs, it binds the UDP port like this:

this.BindPort(5001);

The BindPort method implement blow:

public void BindPort(int port)
{
    m_listener = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
    IPEndPoint Point = new IPEndPoint( IPAddress.Any, Port );
    m_listener.Bind( port);
    m_listener.BeginReceive( buff, 0, buff.Length, SocketFlags.None,
                new AsyncCallback( DataReceived ), buff );
}

And when my server application commands client to bind to another UDP port(e.g. 5005). I call the same BindPort method in client. Then a exception occurs at DataReceived method.

I think there must be a problem that I don't close the UDP port properly. But how can i close the UDP socket properly and rebind to another one. Thanks.

© Stack Overflow or respective owner

Related posts about c#2.0

Related posts about udp