2
votes

To summarize my situation, I am writing a server program that opens a UDP socket with which any number of clients can communicate. I receive UDP packets using code similar to the following:

EndPoint sender = new IPEndPoint(IPAddress.Any, 0);
try
{
    count = socket.ReceiveFrom(buf, ref sender); // 'count' and 'buf' are defined elsewhere
    // If an exception isn't thrown, 'sender' will now contain the EndPoint of the client that sent the packet.
}
catch(SocketException e)
{
    if(e.ErrorCode == 10054)
    {
        // How do I get the EndPoint that caused the error?
        // The 'sender' variable above does not contain the EndPoint.
    }
}

I receive error code 10054 ("An existing connection was forcibly closed by the remote host") when my server sends a packet to a client that has closed its own socket. I would like to stop sending packets to that client so that SocketExceptions stop getting thrown, which severely hurts my server's performance.

But my problem is that I don't know how to get the EndPoint of the particular client that was forcibly closed. (The 'sender' variable above isn't set to anything useful before the exception is thrown.) How can I find that EndPoint?

A less-ideal but still workable solution would be to simply disable the SocketException from being thrown.

Any ideas?

Thanks!

1

1 Answers

2
votes

I guess I got an idea. You should create and maintain a list of your clients andthis list will hold all the endpoints of them (as well as the other useful information you might need).

Data transmission will then happen to "connected" clients whereas all the other yet "unknown" computers will be generally try to "connect" to you though IPAddress.Any.