简体   繁体   English

C#检测客户端与服务器的断开连接

[英]c# detect client disconnection from server

I've got a server and 2 winform clients(for a tictactoegame). 我有一个服务器和2个winform客户端(用于tictactoegame)。 Can I detect a client disconnection instantly ?. 我可以立即检测到客户端断开连接吗? First opened client is player1 socket and 2nd is player2. 首先打开的客户端是player1套接字,第二个是player2。 Once both clients are connected player1 has to click a button and player2 buttons are all disabled. 连接两个客户端后,player1必须单击一个按钮,而player2按钮都将被禁用。 After player1 clicks a button player2buttons are enabled and player1 buttons disabled...so on 在player1单击按钮后,启用player2按钮,并禁用player1按钮...等等

isConnected method isConnected方法

public  bool IsConnected(Socket socket)
{
    try
    {
        return !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0);
    }
    catch (SocketException) { return false; }
}

Server code 服务器代码

public class ServerApp
{
    public Socket serverSocket;

    Socket player1;
    Socket player2;
    ServerGame myGame;

    public ServerApp()
    {
        myGame = new ServerGame();
        serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, 
                                  ProtocolType.Tcp);
        IPEndPoint ipLocal = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8221);
        serverSocket.Bind(ipLocal);
        serverSocket.Listen(4);

        Console.WriteLine("Waiting for clients ...");
        player1 = serverSocket.Accept();
        Console.WriteLine("Player 1 joined");
        player2 = serverSocket.Accept();
        Console.WriteLine("Player 2 joined");

        myGame.InitGame();

        while (true)
        {
            try
            {
                myGame.Display();
                Console.WriteLine();

                myGame.Send(player1, BaseGame.Player.One);
                myGame.Send(player2, BaseGame.Player.Two);

                if (myGame.GetActivePlayer() == BaseGame.Player.One && 
                    myGame.IsConnected(player1)==true)
                {
                    myGame.Receive(player1);                       
                }
                else if(myGame.IsConnected(player1) == true)
                {
                    myGame.Receive(player2);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
                Thread.Sleep(1000);                    
            }
        }

        Console.WriteLine("Server going down");
        Thread.Sleep(2000);
    }
}

class Program
{
    static void Main(string[] args)
    {
        ServerApp serverApp = new ServerApp();
    }
}

The player being closed should gracefully shutdown the socket before exiting. 处于关闭状态的播放器在退出前应适当关闭套接字。 This involves calling Socket.Shutdown(SocketShutdown.Send) , and then waiting for a receive operation to return a byte count of 0 (indicating the remote client has also shutdown). 这涉及到调用Socket.Shutdown(SocketShutdown.Send) ,然后等待接收操作返回字节数0(指示远程客户端也已关闭)。

This will ensure timely closure of the connection. 这样可以确保及时关闭连接。 Otherwise, the other player not being closed will have no way to know that the connection was lost until they attempt to send some data. 否则,其他未关闭的播放器将无法知道连接已丢失,直到他们尝试发送一些数据为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM