gpt4 book ai didi

c# - 连接后如何测试TCPClient连接断开?

转载 作者:可可西里 更新时间:2023-11-01 02:30:33 26 4
gpt4 key购买 nike

我整整3天都在为一个问题而战我找不到任何解决方案,请帮忙:)
我使用 Visual Studio 2010 和 C# 语言。

我有一个像服务器一样工作的设备,它在非常不规则的时间段内发送一些数据(无法定义任何读取超时)。
我编写了一个 TCP 客户端来连接到该服务器并读取数据。它工作正常,但是当网络出现问题并且服务器变得不可用时(例如,当我从计算机上拔下网络电缆时),应用程序需要大约 10 秒才能“注意到”没有连接到服务器并抛出一个异常(exception)。 (我不知道为什么正好10秒?它在哪里定义的?我可以改变它吗?)
我想更快地使用react - 比方说在连接断开后一秒钟之后。
然而,谷歌搜索答案并没有为我提供任何可行的解决方案。

测试代码如下,我尝试在2个线程上进行:一个是读取数据,第二个是寻找连接状态,当它断开时应该提醒我。它对 TCPClient 和 Socket 类都不起作用。我尝试使用 tcpClient.SendTimeout = 100;stream.WriteTimeout = 100; 读取/写入一些数据,但它似乎不起作用。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;

namespace TCPClient
{
class Program
{
static volatile bool _continue = true;
static TcpClient tcpClient;
static NetworkStream stream;

static void Main(string[] args)
{
try
{
//client thread - listen from server
Thread tcpListenThread = new Thread(TcpListenThread);
tcpListenThread.Start();

//connection checking thread
Thread keepAliveThread = new Thread(KeepAliveThread);
keepAliveThread.Start();

while (_continue)
{
if (Console.ReadLine() == "q")
{
_continue = false;
}
}

keepAliveThread.Join(2000);
if (keepAliveThread.IsAlive)
{
Console.WriteLine("Thread keepAlive has been aborted...");
keepAliveThread.Abort();
}

tcpListenThread.Join(2000);
if (tcpListenThread.IsAlive)
{
Console.WriteLine("Listen thread has been aborted...");
tcpListenThread.Abort();
}
}
catch (Exception ex)
{
Console.WriteLine("\n" + ex.Message);
}

Console.WriteLine("\nHit any key to quit...");
Console.Read();
}

private static void TcpListenThread()
{
string server = "172.20.30.40";
int port = 3000;

try
{
using (tcpClient = new TcpClient())
{
tcpClient.Connect(server, port);

if (tcpClient.Connected)
Console.WriteLine("Successfully connected to server");

using (stream = tcpClient.GetStream())
{

while (_continue)
{
Byte[] data = new Byte[1024];
Int32 bytes = stream.Read(data, 0, data.Length);
string responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

Console.WriteLine("Received: {0}, responseData);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Listen thread exception! " + ex.Message);
}
}


private static void KeepAliveThread()
{
while (_continue)
{
if (tcpClient != null)
{
try
{
//...what to put here to check or throw exception when server is not available??...
}
catch (Exception ex)
{
Console.WriteLine("Disconnected...");
}
}

Thread.Sleep(1000); //test for connection every 1000ms
}
}
}
}

编辑:
@carsten 的回答:虽然看起来很有希望,但这个解决方案不起作用......
我为此制作了最简单的测试应用程序:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;

namespace TCPClientTest
{
class Program
{
static void Main(string[] args)
{
try
{
string server = "172.20.30.40";
int port = 3000;

using (TcpClient tcpClient = new TcpClient())
{
tcpClient.Connect(server, port);

int i = 0;
while (true)
{
// This is how you can determine whether a socket is still connected.
bool blockingState = tcpClient.Client.Blocking;
try
{
byte[] tmp = new byte[1];

tcpClient.Client.Blocking = false;
tcpClient.Client.Send(tmp, 0, 0);
Console.WriteLine("Connected!");
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
Console.WriteLine("Still Connected, but the Send would block");
else
{
Console.WriteLine("Disconnected: error code {0}!", e.NativeErrorCode);
}
}
finally
{
tcpClient.Client.Blocking = blockingState;
}

Console.WriteLine("Connected: {0} ({1})", tcpClient.Client.Connected, i++);

Thread.Sleep(1000);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Global exception: {0}", ex.Message);
}
}
}
}

结果如下,显示:

已连接!
已连接:真

每隔一秒加上我的订单号。当我断开网线时,需要 8 秒才能开始打印:

断开连接:错误代码 10054!
已连接:假

所以到 8 秒时,我还没有意识到连接已断开。看起来 ping 是这里的最佳选择,但我将测试其他解决方案。

最佳答案

我认为这是一个经常出现的问题。这可能就是为什么 MSDN 文档确实对此给出了很好的答案 - 请参阅 Socket.Connected

引自那里:

The Connected property gets the connection state of the Socket as of the last I/O operation. When it returns false, the Socket was either never connected, or is no longer connected.

The value of the Connected property reflects the state of the connection as of the most recent operation. If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.

使用此示例代码:

// .Connect throws an exception if unsuccessful
client.Connect(anEndPoint);

// This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking;
try
{
byte [] tmp = new byte[1];

client.Blocking = false;
client.Send(tmp, 0, 0);
Console.WriteLine("Connected!");
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
Console.WriteLine("Still Connected, but the Send would block");
else
{
Console.WriteLine("Disconnected: error code {0}!", e.NativeErrorCode);
}
}
finally
{
client.Blocking = blockingState;
}

Console.WriteLine("Connected: {0}", client.Connected);

以及扩展方法的直接动机:

public static bool IsConnected(this Socket client)
{
// This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking;
try
{
byte [] tmp = new byte[1];

client.Blocking = false;
client.Send(tmp, 0, 0);
return true;
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
return true;
else
{
return false;
}
}
finally
{
client.Blocking = blockingState;
}
}

关于c# - 连接后如何测试TCPClient连接断开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7650402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com