How to recover gracefully from a C# udp socket exception

Posted by Gearoid Murphy on Stack Overflow See other posts from Stack Overflow or by Gearoid Murphy
Published on 2010-04-10T12:25:45Z Indexed on 2010/04/10 12:33 UTC
Read the original article Hit count: 433

Filed under:
|
|

Context: I'm porting a linux perl app to C#, the server listens on a udp port and maintains multiple concurrent dialogs with remote clients via a single udp socket. During testing, I send out high volumes of packets to the udp server, randomly restarting the clients to observe the server registering the new connections. The problem is this: when I kill a udp client, there may still be data on the server destined for that client. When the server tries to send this data, it gets an icmp "no service available" message back and consequently an exception occurs on the socket.

I cannot reuse this socket, when I try to associate a C# async handler with the socket, it complains about the exception, so I have to close and reopen the udp socket on the server port. Is this the only way around this problem?, surely there's some way of "fixing" the udp socket, as technically, UDP sockets shouldn't be aware of the status of a remote socket?

Any help or pointers would be much appreciated. Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about sockets