C# 和 TCP 客户端(异步)具有非常高的 CPU 负载以及如何检测断开连接的客户端?
C# and TCP Client (Async) has very high CPU load & How to detect disconnected clients?
如果多个客户端连接,CPU 加载 90+。
如果我启动监听器并且没有连接,一切都很好。
如果我有一个或多个连接,我的负载会非常高 CPU。
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Test.Socket
{
public class Server
{
List<Thread> WorkListenerThread;
TcpListener Listener;
public Server()
{
WorkListenerThread = new List<Thread>();
}
public void Start()
{
try
{
Listener = new TcpListener(IPAddress.Any, 12345);
Listener.Start();
StartTCPClientListener();
}
catch (Exception) { }
}
private void StartTCPClientListener()
{
Listener.BeginAcceptTcpClient(new AsyncCallback(HandleTCPClientConnection), null);
}
private void HandleTCPClientConnection(IAsyncResult ar)
{
// Problem 1: after the first connection i have a high cpu load
try
{
TcpClient client = Listener.EndAcceptTcpClient(ar);
Thread clientThread = new Thread(Communicator.CreateConnection);
clientThread.Start(client);
WorkListenerThread.Add(clientThread);
StartTCPClientListener(); // Next client
}
catch (Exception) { }
}
public void Stop()
{
foreach (Thread th in WorkListenerThread)
if (th.IsAlive)
th.Abort();
// Problem 2: Exception because the "HandleTCPClientConnection" get a wrong IAsyncResult
if (Listener != null)
Listener.Stop();
}
}
}
还有我的客户class。
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using JsonExSerializer;
namespace Test.Socket
{
class Communicator
{
private TcpClient TcpClient = null;
private NetworkStream Stream = null;
private Communicator(TcpClient client)
{
TcpClient = client;
Stream = TcpClient.GetStream();
StartCommunication();
}
public static void CreateConnection(object c)
{
new Communicator(c as TcpClient);
}
private void StartCommunication()
{
string message; // The message
int bytesRead; // Message length
byte[] buffer = new byte[512]; // buffer
while (true)
{
message = String.Empty;
do
{
bytesRead = Stream.Read(buffer, 0, buffer.Length);
message += Encoding.Default.GetString(buffer, 0, bytesRead);
} while (!message.EndsWith("\r\n.\r\n"));
Send("OK", Stream);
}
}
protected void Send(string message, NetworkStream clientStream)
{
byte[] temp = Encoding.Default.GetBytes(message);
clientStream.Write(temp, 0, temp.Length);
clientStream.Flush();
}
}
}
不知道怎么回事-.-
有谁知道我如何检测客户端是否断开连接?
那也不错。
但修复高 CPU 负载就足够了。
我猜主要问题出在这里:
while (true)
{
message = String.Empty;
do
{
bytesRead = Stream.Read(buffer, 0, buffer.Length);
message += Encoding.Default.GetString(buffer, 0, bytesRead);
} while (!message.EndsWith("\r\n.\r\n"));
Send("OK", Stream);
}
你应该检查这个bytesRead
是否是非正的;如果是,那就是流的结尾,你应该终止:没有更多的数据 ever 来了,但是你在一个紧密的循环中永远追加空字符串。
但是!更大的问题是方法:每个客户端的线程根本无法扩展,并且 不是 人们提到 "async IO".
时的意思
我还要说,拥有一个启动长运行 操作的构造函数(return 也不是)是一件可怕的 事情做。
如果多个客户端连接,CPU 加载 90+。 如果我启动监听器并且没有连接,一切都很好。 如果我有一个或多个连接,我的负载会非常高 CPU。
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Test.Socket
{
public class Server
{
List<Thread> WorkListenerThread;
TcpListener Listener;
public Server()
{
WorkListenerThread = new List<Thread>();
}
public void Start()
{
try
{
Listener = new TcpListener(IPAddress.Any, 12345);
Listener.Start();
StartTCPClientListener();
}
catch (Exception) { }
}
private void StartTCPClientListener()
{
Listener.BeginAcceptTcpClient(new AsyncCallback(HandleTCPClientConnection), null);
}
private void HandleTCPClientConnection(IAsyncResult ar)
{
// Problem 1: after the first connection i have a high cpu load
try
{
TcpClient client = Listener.EndAcceptTcpClient(ar);
Thread clientThread = new Thread(Communicator.CreateConnection);
clientThread.Start(client);
WorkListenerThread.Add(clientThread);
StartTCPClientListener(); // Next client
}
catch (Exception) { }
}
public void Stop()
{
foreach (Thread th in WorkListenerThread)
if (th.IsAlive)
th.Abort();
// Problem 2: Exception because the "HandleTCPClientConnection" get a wrong IAsyncResult
if (Listener != null)
Listener.Stop();
}
}
}
还有我的客户class。
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using JsonExSerializer;
namespace Test.Socket
{
class Communicator
{
private TcpClient TcpClient = null;
private NetworkStream Stream = null;
private Communicator(TcpClient client)
{
TcpClient = client;
Stream = TcpClient.GetStream();
StartCommunication();
}
public static void CreateConnection(object c)
{
new Communicator(c as TcpClient);
}
private void StartCommunication()
{
string message; // The message
int bytesRead; // Message length
byte[] buffer = new byte[512]; // buffer
while (true)
{
message = String.Empty;
do
{
bytesRead = Stream.Read(buffer, 0, buffer.Length);
message += Encoding.Default.GetString(buffer, 0, bytesRead);
} while (!message.EndsWith("\r\n.\r\n"));
Send("OK", Stream);
}
}
protected void Send(string message, NetworkStream clientStream)
{
byte[] temp = Encoding.Default.GetBytes(message);
clientStream.Write(temp, 0, temp.Length);
clientStream.Flush();
}
}
}
不知道怎么回事-.-
有谁知道我如何检测客户端是否断开连接? 那也不错。
但修复高 CPU 负载就足够了。
我猜主要问题出在这里:
while (true)
{
message = String.Empty;
do
{
bytesRead = Stream.Read(buffer, 0, buffer.Length);
message += Encoding.Default.GetString(buffer, 0, bytesRead);
} while (!message.EndsWith("\r\n.\r\n"));
Send("OK", Stream);
}
你应该检查这个bytesRead
是否是非正的;如果是,那就是流的结尾,你应该终止:没有更多的数据 ever 来了,但是你在一个紧密的循环中永远追加空字符串。
但是!更大的问题是方法:每个客户端的线程根本无法扩展,并且 不是 人们提到 "async IO".
时的意思我还要说,拥有一个启动长运行 操作的构造函数(return 也不是)是一件可怕的 事情做。