}
            catch (Exception ex)
            {
                hander.Close();
                hander.Dispose();
            }
catch就不用说了吧 客户端在我设置的时间内没有返回肯定会抛异常 

解决方案 »

  1.   

    不是吧! 断不开?
    我也是最近发现这问题 socket以前做的还是比较多 也没发现
      

  2.   

    不会这个问题还没有人发现吧 其实也是很难发现 我以前也没注意过  现在因为对设备通信 设备用wifi肯定会断开连接断开连接 后来我就发现断开次数太多了 设备就连不上了 在360中强行关闭端口 再打开就又能连接了 当然也可能是设备连接的问题 不过关闭连接还是很有必要的 毕竟志愿还是占用 长时间开启程序那岂不是资源都消耗光了
      

  3.   

    哦,我自己用的可能比较低端吧,就要断开的时候,判断一下是否连接,是连接状态就disConnect,挺好使的,可能我的使用比较简单,
    断开-连接-断开-连接……挺灵的
      

  4.   

    没有发现断不开的情况,楼主还是自行检查代码是否有问题,另外可以看下微软对close方法的推荐执行顺序
    http://technet.microsoft.com/zh-cn/library/wahsac9k(v=vs.100).aspx
      

  5.   


    你是怎么判断它已经断开连接的 在360中显示么?还是
    刚写了一个简单的测试程序 
    服务器端
     private void button1_Click(object sender, EventArgs e)
            {            new Thread((ThreadStart)delegate {recive(); }).Start();
            }
            void recive()
            {            //接收信息
                IPAddress ips = Dns.GetHostAddresses(Dns.GetHostName())[0];
                IPEndPoint ip = new IPEndPoint(IPAddress.Any, 5050);
                Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                soc.Bind(ip);
                soc.Listen(10);
                Socket hander = soc.Accept();
                hander.ReceiveTimeout = 2000;
                try
                {
                    while (true)
                    {
                        byte[] bytes = new byte[1024];
                        int len = hander.Receive(bytes);
                        hander.Send(new byte[] { 0x3A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3B });
                        Thread.Sleep(1000);
                    }
                }
                catch 
                {
                    hander.Close();
                    hander.Dispose();
                }
            }
    客户端
    private void button1_Click(object sender, EventArgs e)
            {
                timer1.Enabled = true;
                td = new Thread(new ThreadStart(send));
                td.IsBackground = true;
                td.Start();        }
            byte[] bt = new byte[1024];
            Socket senSock = null;
            void send()
            {                senSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    
                        try
                        {
                            IPEndPoint sendEndPoint = new IPEndPoint(IPAddress.Parse("192.168.2.128"), 5050);
                            senSock.SendTimeout = 2000;
                            senSock.Connect(sendEndPoint);
                            senSock.Send(new byte[] { 0x3A, 0x03, 0x01, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3B });
                            while (true)
                            {                            int len = senSock.Receive(bt);
                                senSock.Send(new byte[] { 0x3A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3B });
                                Thread.Sleep(1000);
                            }
                        }
                        catch
                        {
                            senSock.Close();
                        }
                    
      }
      

  6.   

    我断开之后,可能要重用套接字,所以是disconnect,从没close过,