C# TCP 服务器模拟

本文关键字:模拟 服务器 TCP | 更新日期: 2023-09-27 18:31:32

我有一个现成的应用程序(客户端),它通过TCP连接到服务器,我使用Wireshark查看发生了什么,并得到了这个:

客户端将此数据包发送到服务器:

|00|18|e7|96|92|13|fc|f8|ae|2b|7a|4b|08|00|45|00|00|3f|6d|d8|00|00|80|11|49|7d|c0|a8|01|07|c0|a8|01|01|c2|b3|00|35|00|2b|cc|7f|5e|fe|01|00|00|01|00|00|00|00|00|00|05|70|61|6e|65|6c|07|6d|75|66|69|62|6f|74|03|6e|65|74|00|00|01|00|01|

服务器回答:

0xfc 0xf8 0xae 0x2b 0x7a 0x4b 0x00 0x18 0xe7 0x96 0x92 0x13 0x08 0x00 0x45 0x28 0x00 0x34 0x00 0x00 0x40 0x00 0x34 0x06 0x2a 0xa4 0x95 0xca 0xc4 0x7e 0xc0 0xa8 0x01 0x07 0x19 0x9b 0xde 0x39 0x18 0x24 0xd5 0x66 0x85 0xa3 0xb1 0x7b 0x80 0x12 0x72 0x10 0xc4 0x81 0x00 0x00 0x02 0x04 0x05 0xac 0x01 0x01 0x04 0x02 0x01 0x03 0x03 0x07

所以我目前的服务器代码是:

Int32 port = 6555;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
var g1 = new byte[] { 0xfc, 0xf8, 0xae, 0x2b, 0x7a, 0x4b, 0x00, 0x18, 0xe7, 0x96, 0x92, 0x13, 0x08, 0x00, 0x45, 0x28, 0x00, 0x34, 0x00, 0x00, 0x40, 0x00, 0x34, 0x06, 0x2a, 0xa4, 0x95, 0xca, 0xc4, 0x7e, 0xc0, 0xa8, 0x01, 0x07, 0x19, 0x9b, 0xde, 0x39, 0x18, 0x24, 0xd5, 0x66, 0x85, 0xa3, 0xb1, 0x7b, 0x80, 0x12, 0x72, 0x10, 0xc4, 0x81, 0x00, 0x00, 0x02, 0x04, 0x05, 0xac, 0x01, 0x01, 0x04, 0x02, 0x01, 0x03, 0x03, 0x07 };
server = new TcpListener(localAddr, port);
server.start();
while(true)
{
     TcpClient client = server.AcceptTcpClient();
     NetworkStream stream = client.GetStream();
     while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
     {
          stream.Write(g1, 0, g1.Length);
     }
 }
 stream.close();
}

因此,每当服务器从客户端收到一些东西时,它都必须发送g1字节(仅用于测试目的),但是在客户端连接到服务器后我收到此错误:

Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.

任何想法都会很棒,谢谢

C# TCP 服务器模拟

我建议在while循环中TcpClient client = server.AcceptTcpClient(); NetworkStream stream = client.GetStream(); 。创建一个新线程,以便在接收消息时不阻塞 UI

private void GetMessageThread()
     {
         bool receive = true;
         Stream strm = client.GetStream();
         while (receive)
         {
             try
             {
                IFormatter formatter = new BinaryFormatter();
                string obj;
                if (strm.CanRead)
                {
                    obj = (string)formatter.Deserialize(strm);
                }
                else
                {
                    _receive = false;
                    break;
                }
           }
         }
    }

在这种情况下,BinaryFormatter 知道流何时结束,因此您不必提供Bytes.Length,也不必在每条消息后关闭流。当服务器或客户端崩溃或在 TcpListener 或 TcpClient 上调用.Close()时,您通常会出错。希望这对你有帮助。