异步读取过程中的TcpClient断开 [英] TcpClient disconnect during async read

查看:544
本文介绍了异步读取过程中的TcpClient断开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对完成TCP连接的几个问题。

I have a few questions about finishing a tcp connection.


  1. 一个客户端使用TCP,连接到我的服务器接受客户端 listener.BeginAcceptTcpClient(ConnectionEstabilishedCallback,NULL)后; ,我开始与<$读C $ C> networkStream.BeginRead(....)。结果
    当客户端断开连接,而我等待消息会发生什么? (例如,其断电,网络等)结果
    如何才能知道当它发生?

如果成功读取后,我做一些东西,然后调用 networkStream.Close(); client.Close(); 什么将在客户端看到了什么?我如何终止连接正常?

If after a successful read, I do some stuff, and then call networkStream.Close(); client.Close(); What will the client see? How do I terminate the connection "gracefully"?

如果我在等待一个读(带的BeginRead),然后(在不同的线程)关闭相同的流会发生什么?

What happens if I'm waiting for a read (with BeginRead), and then (on a different thread) close the same stream?

编辑补充:我有一个乒乓邮件将在客户端和服务器之间。够了吗?如果我没有收到平,终止我的NetworkStream?想必一定有更好的东西。

EDIT TO ADD: I do have a ping-pong message going on between the client and server. Is that enough? If I don't receive ping, terminate my NetworkStream? surely there must be something better.

推荐答案

1如因断开电缆的客户端拔掉你不会知道,直到下一个读取或写入套接字。还注意到,tcpClient.Connected属性值是不可靠的,这是根据在过去的通信值;因此,如果最后的沟通是成功的,然后它的值是true,否则为false。关于那张支票更多信息

1- If the client disconnected due to cable unplugged you will not know till the next read or write to the socket. also note that the tcpClient.Connected property value is not reliable, it's value depending on the last communication; so if the last communication were succeed then it's value is true otherwise it is false. for more information on that check this.

2 - 如果关闭网络流和客户端,这是正常终止客户端。

2- If you close the network stream and the client this is the gracefully termination of the client.

3,我不知道,给它一个考验。

3- I don't know, give it a test.

如果您知道从连接丢失,因为拔掉左右,然后让你必须知道在读过程中丢失或写入TCP连接,所以你需要通过访问TcpClient的成员适当IsConnected价值电缆灌封围绕其运行一个try-catch ....

If you aware from connection lost because of a cable unplugged or so, then to get appropriate IsConnected value you have to be aware from the connection lost during the read or write to the tcp, so you need to access the tcpclient members by potting a try-catch around its operation....

使用此IsConnected属性来检查的TcpClient连接:

Use this IsConnected property to check if the tcpClient is connected:

public static bool IsConnected
{
    get
    {
        try
        {
            //return _tcpClient != null && _tcpClient.Client != null && _tcpClient.Client.Connected;

            if (_tcpClient != null && _tcpClient.Client != null && _tcpClient.Client.Connected)
            {

                /* As the documentation:
                    * When passing SelectMode.SelectRead as a parameter to the Poll method it will return 
                    * -either- true if Socket.Listen(Int32) has been called and a connection is pending;
                    * -or- true if data is available for reading; 
                    * -or- true if the connection has been closed, reset, or terminated; 
                    * otherwise, returns false
                    */

                // Detect if client disconnected
                if (_tcpClient.Client.Poll(0, SelectMode.SelectRead))
                {
                    byte[] buff = new byte[1];
                    if (_tcpClient.Client.Receive(buff, SocketFlags.Peek) == 0)
                    {
                        // Client disconnected
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }

                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }
}

这篇关于异步读取过程中的TcpClient断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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