Abin
Abin

Reputation: 87

Remove IPEndpoint Bind

I have a small form (FORM2) which acts like a pop up window. It takes an IPAddress as an argument and connects to it. The code below shows the connection

myNewUDP.newUDPClient = new UdpClient();
myNewUDP.newEP        = new IPEndPoint(IPAddress.Parse("255.255.255.255"), PORT);
IPEndPoint newBindEP  = new IPEndPoint(IPAddress.Any, PORT);

//Set port to listen to
myNewUDP.newUDPClient.Client.Bind(newBindEP);                                    

When I call the pop up for the first time it connects to the device correctly. However, during the subsequent calls it throws an exception "only one usage of each socket address is permitted ". How can I solve this issue ? Is there a way to close the IP and port binding ?

Upvotes: 0

Views: 943

Answers (1)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31153

Connecting would mean connecting to some other endpoint. Here you are binding, which means you reserve the protocol/port/ip for yourself. To remove the binding call Close() on the socket.

You are also using UdpClient a bit strangely since you are directly calling Bind() on the underlying socket. You could just use a plain Socket then. Or you can use the actual UdpClient to handle everything.

Upvotes: 2

Related Questions