2 个线程处理具有奇怪行为的套接字

2 threads handling sockets with weird behaviour

我刚刚完成了包含 2 个套接字的 C# 代理(一个客户端套接字从某个客户端接收数据,一个服务器套接字从某个服务器接收数据)。

-方法一)

完整工作 receive/send 先前早期 alpha 版本的过程使用以下内存消耗,快速而肮脏的方法我不会再次执行:

            while (true)
            {
                if (ClientSocket.Available > 0)
                {
                    // Received data from the game client
                    byte[] buf = new byte[ClientSocket.Available];
                    ClientSocket.Receive(buf);
                    Packet p = new Packet(buf);
                    Logger.Log(p.ID, LogType.PACKET);
                    // Forward re-encrypted data back to the official server
                    ServerSocket.Send(p.Encrypt());
                }

                if (ServerSocket.Available > 0)
                {
                    // Received Data from the official server
                    byte[] buf = new byte[ServerSocket.Available];
                    ServerSocket.Receive(buf);
                    Packet p = new Packet(buf);
                    Logger.Log(p.ID, LogType.PACKET);
                    // Forward re-encrypted data back to the game client
                    ClientSocket.Send(p.Encrypt());
                }
            }

-方法二)

实际版本中的不工作 receive/send过程使用以下内存友好方法拆分为 2 个线程:

class ClientReceiveThread
{
    public Thread T { get; set; }
    public ClientReceiveThread(Socket ClientSocket, Socket ServerSocket)
    {
        T = new Thread(() =>
        {
            try
            {               
                while (ClientSocket.Available > 0)
                {
                    // Received data from the game client
                    byte[] buf = new byte[ClientSocket.Available];
                    ClientSocket.Receive(buf);
                    Packet p = new Packet(buf);
                    Logger.Log(p.ID, LogType.PACKET);
                    // Forward re-encrypted data back to the official server
                    ServerSocket.Send(p.Encrypt());
                }
            }
            catch (Exception e)
            {
                ExceptionHandler.Handle(e);
            }
        });
        T.Start();
    }
}

class ServerReceiveThread
{
    public Thread T { get; set; }
    public ServerReceiveThread(Socket ClientSocket, Socket ServerSocket)
    {
        T = new Thread(() =>
        {
            try
            {
                while (ServerSocket.Available > 0)
                {
                    // Received Data from the official server
                    byte[] buf = new byte[ServerSocket.Available];
                    ServerSocket.Receive(buf);
                    Packet p = new Packet(buf);
                    Logger.Log(p.ID, LogType.PACKET);
                    // Forward re-encrypted data back to the game client
                    ClientSocket.Send(p.Encrypt());
                }
            }
            catch (Exception e)
            {
                ExceptionHandler.Handle(e);
            }
        });
        T.Start();
    }
}

方法 I) 在 Client 和 Serversockets 都接收数据的情况下工作,而方法 II) 仅从 ClientReceiveThread 接收数据。 为什么方法二)中的ServerReceiveThread收不到数据?它与 while(true) 循环中的代码基本相同,只是移植到一个单独的线程。

非常感谢任何建议或答案。 提前致谢!

通过避免 "Available" 属性:

修复了它
class ClientReceiveThread
{
    public Thread T { get; set; }
    public ClientReceiveThread(Socket ClientSocket, Socket ServerSocket)
    {
        T = new Thread(() =>
        {
            try
            {               
                byte[] buf = new byte[1024];
                while (ClientSocket.Receive(buf) > 0)
                {
                    // Received data from the game client
                    Packet p = new Packet(buf);
                    Logger.Log(p.ID, LogType.PACKET);
                    // Forward re-encrypted data back to the official server
                    ServerSocket.Send(p.Encrypt());
                }
            }
            catch (Exception e)
            {
                ExceptionHandler.Handle(e);
            }
        });
        T.Start();
    }
}

class ServerReceiveThread
{
    public Thread T { get; set; }
    public ServerReceiveThread(Socket ClientSocket, Socket ServerSocket)
    {
        T = new Thread(() =>
        {
            try
            {
                byte[] buf = new byte[1024];
                while (ServerSocket.Receive(buf) > 0)
                {
                    // Received Data from the official server
                    Packet p = new Packet(buf);
                    Logger.Log(p.ID, LogType.PACKET);
                    // Forward re-encrypted data back to the game client
                    ClientSocket.Send(p.Encrypt());
                }
            }
            catch (Exception e)
            {
                ExceptionHandler.Handle(e);
            }
        });
        T.Start();
    }
}