C#TCP服务器模拟

时间:2016-01-23 20:46:25

标签: c# sockets tcp server client

我有一个可以通过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.

任何想法都会很棒,谢谢

1 个答案:

答案 0 :(得分:1)

我建议在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()时出现。 希望这会对你有所帮助。