我使用TcpListener和SocketClient构建了一个基本的.NET服务器 - 客户机基础结构。 它是多线程的和异步的。问题是,当超过30个客户端一次连接时,服务器崩溃。.NET TCP服务器稳定性问题
我还找不到导致崩溃的原因,尽管我使用了相当多的Try-Catch块来确保记录所有的excepions。
所以我在想,我可能在服务器代码的概念上做错了什么。我希望你们能帮我找到造成这些事故的原因。代码如下:
启动服务器,并侦听连接:
public void StartServer()
{
isConnected = true;
listener.Start();
connectionThread = new Thread(new ThreadStart(ListenForConnection));
connectionThread.Start();
}
private void ListenForConnection()
{
while (isConnected)
{
try
{
TcpClient client = listener.AcceptTcpClient();
ClientConnection connection = new ClientConnection(this, client);
connections.Add(connection);
}
catch (Exception ex)
{
log.Log("Exception in ListenForConnection: " + ex.Message, LogType.Exception);
}
}
}
的ClientConnection类:
public class ClientConnection : IClientConnection
{
private TcpClient client;
private ISocketServer server;
private byte[] data;
private object metaData;
public TcpClient TcpClient
{
get { return client; }
}
internal ClientConnection(ISocketServer server, TcpClient client)
{
this.client = client;
this.server = server;
data = new byte[client.ReceiveBufferSize];
lock (client.GetStream())
{
client.GetStream().BeginRead(data, 0, client.ReceiveBufferSize, ReceiveMessage, null);
}
}
internal void ReceiveMessage(IAsyncResult ar)
{
int bytesRead;
try
{
lock (client.GetStream())
{
bytesRead = client.GetStream().EndRead(ar);
}
if (bytesRead < 1)
return;
byte[] toSend = new byte[bytesRead];
for (int i = 0; i < bytesRead; i++)
toSend[i] = data[i];
// Throws an Event with the data in the GUI Dispatcher Thread
server.ReceiveDataFromClient(this, toSend);
lock (client.GetStream())
{
client.GetStream().BeginRead(data, 0, client.ReceiveBufferSize, ReceiveMessage, null);
}
}
catch (Exception ex)
{
Disconnect();
}
}
public void Disconnect()
{
// Disconnect Client
}
}
而从服务器发送到一个数据或所有客户端:
public void SendDataToAll(byte[] data)
{
BinaryWriter writer;
try
{
foreach (IClientConnection connection in connections)
{
writer = new BinaryWriter(connection.TcpClient.GetStream());
writer.Write(data);
writer.Flush();
}
}
catch (Exception ex)
{
// Log
}
}
public void SendDataToOne(IClientConnection client, byte[] data)
{
BinaryWriter writer;
try
{
writer = new BinaryWriter(client.TcpClient.GetStream());
writer.Write(data);
writer.Flush();
}
catch (Exception ex)
{
// Log
}
}
在某些时候,服务器崩溃,我真的没有起点甚至在哪里寻找问题。如果需要,我可以提供更多的代码。
任何帮助是非常赞赏:-) 安德烈
如果您使用的是Win XP SP2,则可能是这种情况。请参阅http://forum.emule-project.net/index.php?showtopic=56016&s=9439bf4074e02abd0a66c1b7dc2d3549 – Zafer 2010-09-08 14:33:32
不,其托管在Windows Server 2003上 – Andrej 2010-09-08 14:37:58
您应该查看未处理的异常事件以尝试捕获异常:http:/ /msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx – 2010-09-08 15:45:21