本文介绍了套接字异步UDP-如何处理客户端崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候所有.

我几乎没有使用C#进行网络编程的经验.
我在将数据发送到客户端的服务器上有一个异步UDP套接字.

如果客户端之一崩溃,我将收到下一个套接字错误:

消息=现有连接已被远程主机强行关闭"
ErrorCode = 10054
SocketErrorCode = ConnectionReset

您如何处理此异常????
我无法关闭套接字,因为还有其他客户端仍在接收消息.我也不能忽略ICMP消息,因为我需要从本地列表中删除客户端.

代码:

Greetings all.

I have almost no experience with Network programming on C#.
I have an Asynchronous UDP socket on a server that sends data to clients.

If one of the client crashes i get the next Socket Error:

Message = "An existing connection was forcibly closed by the remote host"
ErrorCode = 10054
SocketErrorCode = ConnectionReset

How do you handle this exception???
I cant close the socket because there are other clients still receiving messages. I also cant ignore the ICMP message, because i need remove the client from a local list.

The code:

 public void Initialize()
        {
            receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            receiveEndPoint = new IPEndPoint(IPAddress.Any, receivePort);
            receiveSocket.Bind(receiveEndPoint);
            receivePort = (receiveSocket.LocalEndPoint as IPEndPoint).Port;
            receiveBuffer = new byte[BufferSize];
            receiveAsyncResult = receiveSocket.BeginReceiveFrom(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, ref receiveEndPoint, new AsyncCallback(NetworkMessageReceivedCallback), receiveSocket);
          ........
          ......
        }
protected void NetworkMessageReceivedCallback(IAsyncResult asyncResult)
        {
            EndPoint remoteEndPoint = null;
            byte[] bytes = null;
            try
            {
                // Finish receiving the message.
                remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                int bytesRead = receiveSocket.EndReceiveFrom(asyncResult, ref remoteEndPoint);

                // Copy off to a local buffer.
                bytes = new Byte[BufferSize];
                Buffer.BlockCopy(receiveBuffer, 0, bytes, 0, bytesRead);
               // Continue to hear from port
                receiveAsyncResult = receiveSocket.BeginReceiveFrom(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None,
                    ref receiveEndPoint, new AsyncCallback(NetworkMessageReceivedCallback), receiveSocket);
            }
            catch (SocketException socketException)
            {

                if (OnForceDisconnect != null) { OnForceDisconnect(remoteEndPoint); }

               // Continue to hear from port
                receiveAsyncResult = receiveSocket.BeginReceiveFrom(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None,
                    ref receiveEndPoint, new AsyncCallback(NetworkMessageReceivedCallback), receiveSocket);

            }
           ..........
           }



谢谢



Thanks

推荐答案

if (ex.SocketErrorCode == SocketError.ConnectionReset)
{
   JTracer.Write(ex, "connection got broken , nothing serious");
}
   else
{
   pTracer.ThrowError(ex, "begin recieve error");
}


这篇关于套接字异步UDP-如何处理客户端崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:21
查看更多