【发布时间】:2014-09-29 13:18:19
【问题描述】:
我有一个TcpClient,我正在连接到机器,一切工作正常。现在我想通过计时器的帮助在 60 秒内监控连接状态。根据基础研究主题我知道没有直接的方法来测试它。所以我试图通过应用程序离开网络时发送到机器的最近消息的响应来获取它。
这是代码..
// Find out whether the socket is connected to the remote host.
//Send a message to Machine
try
{
byte[] notify = Encoding.ASCII.GetBytes("Hello");
stream.Write(notify, 0, notify.Length);
}catch { }
//Check if it reached to machine or failed
bool getConnectionStatus = client.Connected;
if (getConnectionStatus == true)
{
//Do nothing
}
else
{
//Stop the thread
_shutdownEvent.WaitOne(0);
_thread.Abort();
//Start Again
_thread = new Thread(DoWork);
_thread.Start();
}
但在这种情况下发生的最令人惊讶的事情是,如果机器不在网络中,那么在第一次写入时它也能够写入,这就是为什么连接状态虽然已经断开但仍然显示为已连接网络。第二次尝试发送数据时失败,并且预期状态已断开。
我面临的主要问题是,一旦它与网络断开连接,为什么它能够发送数据。因此,当网络关闭时,我丢失了存储在机器中的所有缓冲区数据. 请帮帮我..
【问题讨论】:
-
发送“PING”,等待“PONG”
-
@AlexK。对不起,我没有得到你
标签: c# networking tcp tcpclient tcp-ip