11

Well, I wonder if some one can help with a problem that I encounter....

I want to close a socket and then rerun from the same port. This is what i am doing...

opening:

    UdpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    UdpServerIpEndPoint = new IPEndPoint(IPAddress.Any, 9050);
    UdpEndPoint = (EndPoint)UdpServerIpEndPoint;
    UdpServer.Bind(UdpServerIpEndPoint);

closeing:

        UdpServer.Shutdown(SocketShutdown.Both);
        UdpServer.Disconnect(true);
        UdpServer.Close();

After I close it. and the I try to reconnect it with the same code as above, I get error:

Additional information: Only one usage of each socket address (protocol/network address/port) is normally permitted

I checked for exception during closing, but I didnt get any, i guessed they were closed properly, so actually, what is causing this problem? Please help!

Jasim Khan Afridi
  • 776
  • 3
  • 15
  • 28
  • Can you add your code where you try to reconnect? – Tim Oct 30 '11 at 01:48
  • its the same as mentioned above.... Actually, its under a thread. I stop the thread, then socket it closed. and then i press connect and thread starts with the same settings – Jasim Khan Afridi Oct 30 '11 at 01:52
  • Do you really execute UdpServer.Disconnect(true) successfully? MSDN says that Disconnect() can be used with connection-oriented protocols. UDP is a connectionless protocol. – Hans Oct 30 '11 at 07:38

3 Answers3

17

I got answer.... I need to use this after decleration of socket...

socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress, true);
Jasim Khan Afridi
  • 776
  • 3
  • 15
  • 28
0

For reference, in case anyone else stumbles onto this question but looking for the UdpClient implementation.

int port = 1234;
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
var endPoint = new IPEndPoint(IPAddress.Any, port);
socket.Bind(endPoint);

var updClient = new UdpClient();
updClient.Client = socket;
updClient.Connect(_ipAddress, port);
Miebster
  • 2,365
  • 4
  • 21
  • 27
  • Don't call `Connect` on your `UdpClient` as UDP is connectionless. – Slate Feb 18 '16 at 14:18
  • @kjhf UDP may be connection-less, but does that mean that Microsoft's implementation of the C# UdpClient is? https://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.connect(v=vs.110).aspx – Miebster Feb 18 '16 at 21:47
0

You could look at this 2 ways.

  1. Don't actually Receive whilst your are in your stopped state, just in your BeginReceive, just drop/ignore the data

  2. If you really need to recreate the socket, then don't perform the disconnect on your Server because you haven't actually got a connection. Your disconnect is throwing

System.Net.Sockets.SocketException (0x80004005): A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

Paul Farry
  • 4,730
  • 2
  • 35
  • 61
  • OK, I removed disconnect, still, its not working. I get this exception while reconnecting "Additional information: You must call the Bind method before performing this operation."... – Jasim Khan Afridi Oct 30 '11 at 14:42
  • I can't seem to reproduce any issue with the revised code when the disconnect is removed. Only other option may be to try using the Close(0); to close immediately. – Paul Farry Oct 30 '11 at 22:32