【问题标题】:Client disconnected, but server don't see it客户端断开连接,但服务器看不到它
【发布时间】:2018-07-07 06:00:02
【问题描述】:

所以当我在客户端断开连接时遇到问题时,我正在制作 Tcp 服务器。正在抛出异常,但看起来该服务器没有看到它。它不会从列表中删除断开连接的客户端。这是代码:

    public void Read(IAsyncResult ar, TcpClient PL)
    {
        try
        {
            {
                BytesRead[PL] = PL.GetStream().EndRead(ar);
            }
            if (BytesRead[PL] < 1)
            {
                throw new SocketException();

            }
            else
            {
                while (true)
                {
                    packets++; break;
                }
            }
        }
        catch (Exception x)
        {
            if ((x.Message.Contains("Client") && x.Message.Contains("Disconnected")) || x is SocketException || x is EndOfStreamException || x is IOException)  //|| x.InnerException.GetType() == typeof(IOException))
            {
               throw;
            }
            else
            {
                System.Console.WriteLine(x.ToString());
            }
        }
    }

当客户端关闭程序或被任务管理器杀死时发生

这里是使用Read方法的地方。

    private void HandleClient(object client)
    {

        TcpClient tcpClient = (TcpClient)client;
        NetworkStream ns = tcpClient.GetStream();
        while (true)
        {

            try
            {
                while ((true))
                {
                    {
                        

                        try
                        {

                            
                        }
                        catch (SocketException ex)
                        {
                            if ((ex.Message.Contains("Client") && ex.Message.Contains("Disconnected")) || ex is SocketException)
                            {
                                throw;
                            }
                        }
                        try
                        {
         
                            {
                                
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            catch (SocketException ex)
            {
            
                if (((ex.Message.Contains("Client") && ex.Message.Contains("Disconnected"))) || ex is SocketException )
                {
                    clients.Remove(tcpClient);
                }
                else
                {
                    System.Console.WriteLine(ex.ToString());
                }
            }
        }
    }

这里是TcpListener初始化

    public void Server1()
    {
        timer = new Timer(new TimerCallback(MultipleCheck), null, 0, 500);
        listener = new TcpListener(IPAddress.Parse("25.75.22.56"), 29339);
        listenThread = new Thread(new ThreadStart(ListenForClients));
        listenThread.Start();
    }

这是接受TcpClient的方法

    private void ListenForClients()
    {

        listener.Start();

        while (true)
        {
            System.Console.Title = "Server : " + clients.Count.ToString();
            if (!LineStarted)
            {
                System.Console.WriteLine("Server started on port 29339");
                LineStarted = true;
            }
            System.Console.WriteLine("Connection Request");
            TcpClient client =  listener.AcceptTcpClient();
            if (client.Connected)
            {
                clients.Add(client);
                System.Console.WriteLine("New Client Connected");
                
                //if (threads.ContainsKey(client))
                {
                    threads[client].Start(client);
                }
                counter++;
                
            }

            
        }
    }

【问题讨论】:

  • 你有异常和堆栈跟踪吗?哪条线路造成了问题?
  • BytesRead[PL] = PL.GetStream().EndRead(ar);这个
  • 你能显示代码吗,当你使用void Read(IAsyncResult ar, TcpClient PL)方法时,初始化TcpClient
  • “通常需要多长时间才能有人回答这个问题?” -- 这取决于问题。对于直截了当、写得很好的问题,包括可靠地重现问题的良好minimal reproducible example,可能会相对较快地提供答案。通常不到一个小时,在许多情况下,至少不到半天左右。像你这样的问题?您可能永远不会看到发布的答案,即使发布了答案,它也可能对您有用,也可能不会。
  • 请记住,您的代码比您的代码更难理解,这使我们更难诊断问题。就像@PeterDuniho 说的那样,MCVE 是首选,以便我们更轻松地为您提供帮助。通过重新创建您的代码的较小版本,您有时甚至可以自己发现问题而不必提出问题。 :)

标签: c# .net sockets exception tcp


【解决方案1】:

threads[tcpClient].Abort(); 之前的 put try 指令

try
{
//Removing Client from list and notifying connected users about disconnected 
//client from list
}
catch
{
}
//And after Abort the thread to avoid the exception of thread aborting being
//thrown too early
threads[tcpClient].Abort();
threads.Remove(tcpClient);

【讨论】:

    猜你喜欢
    • 2020-07-21
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2015-05-17
    • 2013-03-31
    相关资源
    最近更新 更多