views:

124

answers:

2

How to Free a Port in .NET used by TCPListener.... after a TCPListener has recieved a required thing from

A: 

We first established the first Socket:

     Socket socket1; 
     IPEndPoint localEP = new IPEndPoint (IPAddress.Any, 20000); 
     Socket1 = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
     Socket1.Bind (localEP);

Then a second Socket:

     Socket socket2 
     IPEndPoint localEP = new IPEndPoint (IPAddress.Any, 20000); 
     Socket2 = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
     Socket2.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 
    / / Please note this one. True ReuseAddress options will be set to allow the socket to bind already in use in the address. 
     Socket2.Bind (localEP);

This will be bundled Socket1 and Socket2 in the same port on the.

+2  A: 

Just call the Stop-method on the TcpListener.

Tommy Carlier