I’m using a UdpClient at the server end and it is sending data to the client end (more than one client).
Suddenly the client stops listening on the udp port and the server gets hit with an SocketException, either when calling endRecieve or beginRecieve.
To my understanding this is because of an “ICMP Destination Unreachable” and it’s just telling the server the port’s closed. That’s ok, but neither of the SocketExceptions tell me which endpoint it is from.
How can I know which endpoint is closed so the server stops sending to it and causing more SocketExceptions?
Or is there a way for Udpclient to stop throwing these SocketExceptions so I can make the clients timeout if they don’t respond after so and so seconds.
I’m dealing with the same issue myself so I’ll be interested to see if anyone comes up with a better solution, but for now I have a couple ideas:
I have a comm wrapper class (let’s call it
AsyncComm) around my sockets that is passed an exception handler delegate from its owner class when it’s constructed. The exception handler delegate takes arguments of an exception and a reference to theAsyncComminstance that threw it. I then putin each of my async handler methods in
AsyncCommso they can throw their exceptions up the chain. In my case, the exception handler uses the reference to theAsyncComminstance to call a method in theAsyncComminstance to tell it to reinitialize its socket. You can change that behavior to whatever you need to do to stop continuously gettingSocketExceptions.Regarding determining the end point the exception came from, the only idea I have right now is parsing the end point from the end of the
SocketException.Messagestring, but that seems like quite a kludge.Update: It is a kludge but it works. Parse code below, some of it taken from this question.