TCP Listener Asnchronous不关闭客户端成功对象处置异常 [英] TCP Listener Asnchronous not close client successfully object dispose exception

查看:83
本文介绍了TCP Listener Asnchronous不关闭客户端成功对象处置异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请验证我的代码。我正在异步处理tcp监听器。当我与所有客户断开连接时,它会出现以下错误:

无法访问已处置的对象。

对象名:System.Net.Sockets.Socket 。




Please verify my code. I am working on tcp listener asynchronously. When i disconnect from all of my clients it gives the following error:
"Cannot access a disposed object.
Object name: System.Net.Sockets.Socket.
"

 public static void StopServer()
        {
            try
            {
                Status = false;
                listenerSocket.Stop();
                backgroundworkerListener.CancelAsync();
                backgroundworkerListener.Dispose();

                if (ClientDevices != null)
                {
                    foreach (ClientManager mngr in ClientDevices)
                    {
                        mngr.Disconnect();
                    }
                    ClientDevices.Clear();
                }
                GC.Collect();
            }
            catch (ObjectDisposedException ode)
            {
                Console.WriteLine(ode.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }


public bool Disconnect()
       {
           if (this.tcpClient != null )
           {
               try
               {
                   SocketAsyncEventArgs e = new SocketAsyncEventArgs();
                   this.networkStream.Flush();
                   this.networkStream.Close();
                   this.networkStream.Dispose();
                   this.tcpClient.Close();
                   this.tcpClient.Client.Close();
                  // this.tcpClient.Client.DisconnectAsync(e);
                   tcpClient.Client.Shutdown(SocketShutdown.Both);
                   GC.Collect();
                   return true;
               }
               catch (ObjectDisposedException oex)
               {
                   throw new ObjectDisposedException(base.GetType().FullName);
                 //  return false;
               }
               catch
               {
                   return false;
               }
           }
           else
               return true;
       }

推荐答案

我解决了实际关机必须在网络流关闭之前调用。

谢谢Rahul
I resolved actually shutdown must be call before network stream closed.
Thanks Rahul


使用这个



Use this one

public bool Disconnect()
       {
           if (this.tcpClient != null )
           {
               try
               {
                   SocketAsyncEventArgs e = new SocketAsyncEventArgs();
                   this.networkStream.Flush();
                   this.networkStream.Close();
                   this.networkStream.Dispose(); 
                   //this.tcpClient.Client.DisconnectAsync(e);
                   tcpClient.Client.Shutdown(SocketShutdown.Both);
                   this.tcpClient.Client.Close();
                   this.tcpClient.Close();
                   GC.Collect();
                   return true;
               }
               catch (ObjectDisposedException oex)
               {
                   throw new ObjectDisposedException(base.GetType().FullName);
                 //  return false;
               }
               catch
               {
                   return false;
               }
           }
           else
               return true;
       }


这篇关于TCP Listener Asnchronous不关闭客户端成功对象处置异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆