小编Rhe*_*zon的帖子

C#UDP远程主机强制关闭现有连接

我正在为UDP使用异步方法处理多个客户端的游戏创建一个服务器,并且我专门研究干净的断开逻辑.当客户端硬崩溃(他们的程序在没有正确的断开逻辑的情况下关闭)readCallback时,服务器上会抛出SocketException

远程主机强制关闭现有连接

这是有道理的,但是当读被触发循环的下一次read崩溃,尽管异常回调被处理.

    private void connectedState()
    {
        while (connected)
        {
            //reset the trigger to non-signaled
            readDone.Reset();

            read(socket);

            //block on reading data
            readDone.WaitOne();
        }
    }


    private void read(Socket sock)
    {
        // Creates an IpEndPoint to capture the identity of the sending host.
        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
        EndPoint senderRemote = sender;

        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = sock;

        //crashes after an exception is caught within the callback
        sock.BeginReceiveFrom(state.buffer, …
Run Code Online (Sandbox Code Playgroud)

c# udp network-programming

5
推荐指数
1
解决办法
3650
查看次数

标签 统计

c# ×1

network-programming ×1

udp ×1