简体   繁体   English

服务器中收到的消息不正确

[英]Inaccurate received message in the server

I have an issue with receiving messages in client-server. 我在客户端服务器中接收消息时遇到问题。 I have set a default string for testing. 我已经设置了用于测试的默认字符串。 It receives the message just fine but it sticks the string together when it sends the second loop. 它收到的消息很好,但是在发送第二个循环时,它将字符串粘在一起。 What should i do with this? 我该怎么办?

Here is the code for sending data(client) 这是发送数据的代码(客户端)

NetworkStream networkStream = tcpclnt.GetStream();
String data = "";
     for (int i = 0; i < 4; i++)
         {
           data = " output here";
           if (networkStream.CanWrite)
              {
                var bytes = Encoding.ASCII.GetBytes(data);
                networkStream.Write(bytes, 0, bytes.Length);
                networkStream.Flush();
              }
          }

And here when it receives the data(server) 当它收到数据时(服务器)

private void recieveData()
{
  NetworkStream nStream = tcpClient.GetStream();
  while (!stopReceiving)
      {
         if (nStream.CanRead)
           {
              byte[] buffer = new byte[1024];
              var bytesRead = nStream.Read(buffer, 0, buffer.Length);
              if (bytesRead > 0) recvDt = Encoding.ASCII.GetString(buffer, 0, bytesRead);
              bool f = false;
              f = recvDt.Contains("+@@+");
                    if (f)
                    {
                        string d = "+@@+";
                        recvDt = recvDt.TrimStart(d.ToCharArray());
                        clientDis();
                        stopReceiving = true;
                    }


                    else 
                    {
                        this.Invoke(new rcvData(addHere));
                    }
                }
                Thread.Sleep(1000);
            }

        }


public void addHere() 
        {
            if (recvDt != null && recvDt != "")
            {
                output.Text += "Received Data: " + recvDt;
                recvDt = null;
            }

        }

The output looks like this 输出看起来像这样

You are assuming that TCP preserves your messages. 您假设TCP保留了您的消息。 It does not. 它不是。 TCP offers a boundaryless stream of bytes. TCP提供了无边界的字节流。 You can receive bytes in any chunk size, including one, but also including all of them at once. 您可以接收任何块大小的字节,包括一个字节,但也可以一次包含所有字节。

You need to devise an application protocol that allows you to tell messages apart from that stream of bytes. 您需要设计一个应用程序协议,该协议允许您区分字节流之外的消息。 Maybe, prefix the message with its length. 也许给消息加上前缀。

Or, send line-wise and use StreamReader.ReadLine. 或者,按行发送并使用StreamReader.ReadLine。 It handles this for you. 它为您处理。

This is a bug: 这是一个错误:

                    string d = "+@@+";
                    recvDt = recvDt.TrimStart(d.ToCharArray());

You are basically trimming away all chars "+@". 基本上,您正在修剪所有字符“ + @”。 Not what you wanted. 不是您想要的。

Why are you testing CanRead and CanWrite ? 为什么要测试CanReadCanWrite They are known to be true and even if they were false your program would just ignore the problem and keep going doing nothing. 众所周知它们是正确的,即使它们是错误的,您的程序也只会忽略该问题,并且什么也不做。 Don't do superstitious checks like that if you can't handle the result anyway. 如果您仍然无法处理结果,请不要进行这样的迷信检查。

Probably, you should not use TCP at all. 可能您根本不应该使用TCP。 It's hard. 这个很难(硬。 Use a higher level technology such as WCF, HTTP, protobuf. 使用更高级别的技术,例如WCF,HTTP,protobuf。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM