gpt4 book ai didi

c# - 关闭 TcpClient 和 NetworkStream 的问题

转载 作者:太空狗 更新时间:2023-10-30 01:16:11 27 4
gpt4 key购买 nike

目前我正在开发一个简单的客户端服务器聊天程序(想使用 C# 进行客户端服务器通信)。到目前为止,除了与服务器正确断开连接外,它一直有效。

在客户端,我在这里使用这段代码来关闭连接:

client.Client.Disconnect(false); // client is the TcpClient
client.Close();

在服务器上有一个线程循环等待来自客户端的消息:

private void StartChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
string rCount = null;

while (true)
{
try
{
requestCount++;

NetworkStream stream = tcpClient.GetStream();

int bufferSize = (int)tcpClient.ReceiveBufferSize;
if (bufferSize > bytesFrom.Length)
{
bufferSize = bytesFrom.Length;
}

stream.Read(bytesFrom, 0, bufferSize);
dataFromClient = System.Text.Encoding.UTF8.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
rCount = Convert.ToString(requestCount);

string message = client.Name + " says: " + dataFromClient;
program.Broadcast(message);

}
catch(Exception ex) when (ex is ObjectDisposedException || ex is InvalidOperationException || ex is System.IO.IOException)
{
program.UserDisconnected(client);
break;
}
catch(ArgumentOutOfRangeException ex)
{
Debug.WriteLine(ex.ToString());
break;
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
break;
}
}

如果客户端与上面显示的代码断开连接,函数将一直获取流并产生这样的输出:

\0\0\0\0\0\0\0 [等等]

在这种情况下,将抛出 ArgumentOutOfRangeException,因为没有 $ 的索引。我添加了一个 break 来避免无休止地执行循环。

令人惊讶的是 ObjectDisposedException 不会被抛出。此外,不会抛出 System.IO.IOException,但它应该抛出,因为流已关闭,因此连接已被拒绝。

如果我只是关闭连接到服务器的客户端应用程序,服务器不会停止循环,它只是等待一个流,该流永远不会到来,因为客户端断开连接。

那么我如何检测客户端是否已断开连接或无法再访问?我关闭客户端连接的方式是否是关闭连接的正确方式?

感谢您的帮助!

更新:

private void StartChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
string rCount = null;

while (true)
{
try
{
requestCount++;

NetworkStream stream = tcpClient.GetStream();
stream.ReadTimeout = 4000;

int bufferSize = (int)tcpClient.ReceiveBufferSize;
if (bufferSize > bytesFrom.Length)
{
bufferSize = bytesFrom.Length;
}


// Wait for a client message. If no message is recieved within the ReadTimeout a IOException will be thrown
try
{
int bytesRead = stream.Read(bytesFrom, 0, bufferSize);
stream.Flush();

if (bytesRead == 0)
{
throw new System.IO.IOException("Connection seems to be refused or closed.");
}
}
catch (System.IO.IOException)
{
byte[] ping = System.Text.Encoding.UTF8.GetBytes("%");
stream.WriteTimeout = 1;

stream.Write(ping, 0, ping.Length);
continue;
}


dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
rCount = Convert.ToString(requestCount);

string message = client.Name + " says: " + dataFromClient;
program.Broadcast(message);

}
catch(Exception ex) when (ex is ObjectDisposedException || ex is InvalidOperationException || ex is System.IO.IOException)
{
Debug.WriteLine(ex.ToString());
program.UserDisconnected(client);
break;
}
catch(ArgumentOutOfRangeException ex)
{
Debug.WriteLine(ex.ToString());
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
break;
}
}
}

最佳答案

您应该检查 stream.Read 的返回值 - 它返回实际读取的字节数。

当客户端断开连接时,这将为 0,当它读取数据时,读取的字节数通常小于缓冲区大小。

int bytes = stream.Read(bytesFrom, 0, bufferSize);
if (bytes == 0)
{
// client has disconnected
break;
}

dataFromClient = System.Text.Encoding.UTF8.GetString(bytesFrom, 0, bytes);

针对您最近的评论,当客户端终止其连接时可能会发生三种情况:

  • 客户端正确关闭连接,你收到0字节
  • 发生了可检测的事情,导致了异常
  • 客户端“消失”了,但是没有信号发送到你这边

在最后一种情况下,服务器仍然会像存在事件连接一样工作,直到它尝试发送数据,这显然会失败。这就是为什么许多协议(protocol)使用连接超时和/或保持事件机制的原因。

关于c# - 关闭 TcpClient 和 NetworkStream 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36092245/

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