【发布时间】:2016-03-18 18:52:32
【问题描述】:
目前我正在开发一个简单的客户端服务器聊天程序(想在 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 [and so on]
在这种情况下,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;
}
}
}
【问题讨论】:
标签: c# tcpclient networkstream