继续说明一下:对于s.Poll(-1, SelectMode.SelectRead)中参数 -1 的设置,我个人觉得不好,如果连接保持但是无数据传输,那么此处会等待!   我个人的做法是s.Poll(20, SelectMode.SelectRead),不知这样是否正确...?

解决方案 »

  1.   

         继续补充:若 接收缓冲区 中有数据,则s.Poll(20, SelectMode.SelectRead)依然应该返回True,那么int nRead = s.Receive();会读走 接收缓冲区 中的数据。若不想在判断Socket是否依然连接的时候读取数据,用Socket.Available是否能行得通?
      

  2.   

    你贴出的msdn不是已经说明了嘛!
      

  3.   

    正常的Receive是在子线程里的,无需你所谓的那种“检测”流程。在Receive出现异常时就完全终止接受就行了。如果你非要去额外地“检测”,msdn说的很明确“如果您需要确定连接的当前状态,请进行非阻止、零字节的 Send 调用”。
      

  4.   


    这些方法都不是很可靠。如果你使用Intel的服务器网卡,它有硬件的诊断功能,用法在Intel开发者网站有。别的网卡就不行了。
      

  5.   

    根据“如果您需要确定连接的当前状态,请进行非阻止、零字节的 Send 调用”的检测代码只能判断对方由连接状态直接关闭软件! 不能检测出对方断开连接(tcpClient.Close)!我测试过好多次了...之所以要“检测”实时连接状态,是因为要保持连接,若连接断开则自动再次连接;连接的过程中绝大多数时间不会进行读写,而且客户端是接收到 设定的信息 才会Send,所以才会去“检测”实时连接状态
      

  6.   

    根据“如果您需要确定连接的当前状态,请进行非阻止、零字节的 Send 调用”的检测代码只能判断对方由连接状态直接关闭软件! 不能检测出对方断开连接(tcpClient.Close)!我测试过好多次了...之所以要“检测”实时连接状态,是因为要保持连接,若连接断开则自动再次连接;连接的过程中绝大多数时间不会进行读写,而且客户端是接收到 设定的信息 才会Send,所以才会去“检测”实时连接状态
    参考这个博客来实现:http://www.cnblogs.com/zhili/archive/2012/08/25/TCP.html
      

  7.   

    public bool Connected
            {
                get
                {
                    if (tcpc == null)
                        return false;
                    // 另外说明:tcpc.Connected同tcpc.Client.Connected;
                    // tcpc.Client.Connected只能表示Socket上次操作(send,recieve,connect等)时是否能正确连接到资源,
                    // 不能用来表示Socket的实时连接状态。
                    try
                    {
                        if ((tcpc.Client.Poll(20, SelectMode.SelectRead)) && (tcpc.Client.Available == 0))
                            return false;
                    }
                    //catch (SocketException e)
                    //{
                    //    if (e.NativeErrorCode != 10035)
                    //        return false;
                    //}
                    catch
                    {
                        return false;
                    }
                    return true;
                }
            }
      

  8.   

    TcpClient tcpClient = (TcpClient)client;
    NetworkStream netstream = tcpClient.GetStream();
    StreamReader reader = new StreamReader(netstream);
    string temp = "";
    temp = reader.ReadLine();
    if (temp == null)
    {
        MessageBox.Show("连接断开");
    }
    else
    {
        MessageBox.Show(temp);
    }
    不管是客户端,人为的关闭连接 ,或者客户端,直接关闭程序,都会弹出连接断开
    FORMCLOSEING事件 不要加TCPCLIENT.CLOSE();可以测试
      

  9.   

     private void connect()
            {
                while (true)
                {
                    tcpclient = new TcpClient();
                    int count = 0;
                    while (tcpclient.Connected == false)
                    {
                        try
                        {
                            tcpclient.Connect(IPAddress.Loopback, 8888);
                            if (tcpclient.Connected)
                                break;
                        }
                        catch (Exception ex)
                        {
                        }
                        }
                    }
                    WriteStream = tcpclient.GetStream();
                    ReadStream = tcpclient.GetStream();
                    recv_Message();
                    while (tcpclient.Connected)
                    {
                    }
                }
            }
    此函数的功能是当服务器关闭程序后,客户端能自动连接重启后的服务器。但是当把recv_Message()函数去掉后,tcpclient.Connected总是为true,请问是什么原因?
      

  10.   

    你贴的代码中  根本没有recv_Message()这个方法的原型 没法回答   你是在recv_Message()中做了什么?
      

  11.   

    刚看到了一篇文章  有所启发
    http://hi.baidu.com/gaoxiaotiger/item/d337d2245a3468cea5275ac0
      

  12.   

    http://social.msdn.microsoft.com/Forums/windowsazure/en-US/203e904d-b581-4e53-81b6-86a4b71cf5f5/keeping-tcpclient-client-connected?forum=netfxnetcomI've read that document before. And implemented the line: 
    Code
    Code Snippet
    StreamWrite("", NWStream2); Where the "" is a string to be sent. However this did not make a diffrence. I actually had to send something. I changed it toCode Snippet
    StreamWrite("0", NWStream2);This not only keeps the socket alive, it also throws the exception I was looking for.