2010-09-08 90 views
1

我使用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 
     } 
    } 

在某些时候,服务器崩溃,我真的没有起点甚至在哪里寻找问题。如果需要,我可以提供更多的代码。

任何帮助是非常赞赏:-) 安德烈

+0

如果您使用的是Win XP SP2,则可能是这种情况。请参阅http://forum.emule-project.net/index.php?showtopic=56016&s=9439bf4074e02abd0a66c1b7dc2d3549 – Zafer 2010-09-08 14:33:32

+0

不,其托管在Windows Server 2003上 – Andrej 2010-09-08 14:37:58

+0

您应该查看未处理的异常事件以尝试捕获异常:http:/ /msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx – 2010-09-08 15:45:21

回答

1

你应该访问连接领域线程安全的。

在SendData中,您正在遍历连接并向每个客户端发送数据。如果一个新的客户端在foreach循环执行时连接,你将得到一个异常,并显示消息“Collection was modified; enumeration operation may not execute”,因为当你迭代它时这个集合就被修改了,这是不允许的。

修改SendDataToAll行至

foreach (IClientConnection connection in connections.ToList()) 

,使问题消失(解决方案是Collection was modified; enumeration operation may not execute提供)。

+0

谢谢,看起来很有希望,我会看看是否有帮助:-) – Andrej 2010-09-09 10:12:17

+0

似乎这部分真的导致一些崩溃。不知道他们是否都走了,但这真的很有帮助。谢谢。 – Andrej 2010-09-10 13:16:56

0

ClientConnection.ReceiveMessage方法的catch块中的Disconnect调用有可能抛出一个异常,然后该异常在catch中传播并且未处理。

为了确保捕获所有异常并记录它们,请尝试向AppDomain.CurrentDomain.UnhandledException事件注册一个事件处理程序。另外,如果您将服务器作为Windows服务运行,则应用程序事件日志中可能存在.NET异常条目。

+0

不,Disconnect中的代码也在Try Catch块中。 – Andrej 2010-09-09 10:11:07