在TCP客户端接收数据的格式不正确 [英] Receiving data at TCP Client is not in proper format

查看:135
本文介绍了在TCP客户端接收数据的格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i写一个能够与服务器通信的TCP客户端,但每次我得到不同的字符串。

RT832

RT

RT8

RT832

R



但是在超级终端我总是得到正确的字符串像

RT832

RT832

RT832

RT832



低于我的代码:

Hi ALL,

i write a TCP client which able to communicate with Server but each time i am getting different-different string .
RT832
RT
RT8
RT832
R

but on hyperterminal i am always getting proper string like
RT832
RT832
RT832
RT832

below my code:

private static void Main(string[] args)
       {
           TcpClient ourMagicClient = new TcpClient();

           //Connect to the server - change this IP address to match your server's IP!
           ourMagicClient.Connect("192.123.0.254", 7000);

           //Use a NetworkStream object to send and/or receive some data
           NetworkStream ns = ourMagicClient.GetStream();

           byte[] fromClient = new byte[10025];
           byte[] toClient;
           while (ns.Read(fromClient, 0, (int) ourMagicClient.ReceiveBufferSize) > 0)
           {
               int nub = ns.Read(fromClient, 0, (int)ourMagicClient.ReceiveBufferSize);
               string str = Encoding.ASCII.GetString(fromClient, 0, nub);
               Console.Write(str);
               toClient = fromClient;
              ns.Write(toClient, 0, toClient.Length);
              ns.Flush();
           }
           Console.ReadKey();
       }



先谢谢。

jmd: - )


Thanks in advanced.
jmd :-)

推荐答案

您会收到随机结果,因为您编写了一些协议以获得随机结果,更准确地说,是随机边界处的相同数据。如果您使用的不是ASCII而是一些具有不同数据类型的二进制数据(甚至某些具有不同字符大小的Unicode UTF,例如UTF-8),您将获得不同或无效的内容。现在你只能获得可识别的字符,因为数据元素之间的边界是1个字节。



整个方法都是错误的。你不应该读取缓冲区中的所有内容,或者,如果你真的想要它,你应该等到你得到足够的字符(但你真的不需要它)。这一切都取决于RS-232电缆另一端的情况,但通常您需要定义一些应用程序级协议,您可以在其中交换已知大小的数据块或通过协议传输的大小值在数据本身之前。



如果你无法改变发送数据的设备的行为,你应该阅读所有内容,你有两个选择:1)
准确读出预期的内容;如果对方没有准备好;你的线程将进入等待状态,直到数据到达缓冲区;这就是使用单独的线程进行通信的原因,这总是一个好主意;在这种情况下,您还应该为所有读取循环的结束定义一些条件; 2)你可以定期读取缓冲区内容,但是你需要保存数据,并且在你拥有足够大小的缓冲区副本之后解析;例如,如果您希望每个字符串包含5个字符的块,那么当您有一个或多个5个字符的块时,请稍后再执行此操作。这段看起来有点不确定,但只是因为你没有解释你的协议,因为它应该是。希望你能得到一个正确的想法。



-SA
You receive random results because you programmed some protocol to get random results, more exactly, the same data at random boundaries. If you used not ASCII but some binary data with different data types (or even some Unicode UTF with different character size, such as UTF-8), you would get different or invalid content. Right now you get recognizable characters only because your boundary between data elements is 1 byte.

The whole approach is wrong. You should not read all you have in the buffer, or, if you really want it, you should wait until you get enough characters (but you don''t really need it). It all depends on what is going on on the other end of your RS-232 cable, but normally you would need to define some application-level protocol where you exchange the chunks of data of known size, or the size values transmitted through the protocol before data itself.

If you cannot change the behavior of the device sending your the data, and you should read everything, you have two options: 1)
read exactly what''s expected; if the other side is not ready; your thread will get to wait state until data arrives to the buffer; this would be the reason to use a separate thread for communication, which is always a good idea; in this case, you also should define some conditions for the end of all the reading loop; 2) you can read the buffer content periodically, but then you would need to save the data and "parse" is after you have the copy of the buffer of sufficient size; for example, if you expect chunks of string 5 characters each, do it later, when you have one or more chunks of 5 characters. This paragraph looks somewhat uncertain, but only because you did not explain your protocol as it is supposed to be. Hope you can get a right idea.

—SA


你必须阅读的实现写字符串有点不必要。当我做这种事情时,我将 StreamReader StreamWriter 附加到的NetworkStream



您的流阅读器有一个方便的ReadLine()方法,并且编写器有一个WriteLine(),因此您需要在缓冲区/编码级别工作,但是只是用字符串。
The implementation you have to read and write strings is a bit unnecessary. When I do this sort of thing, I attach both a StreamReader and a StreamWriter to the NetworkStream.

Your stream reader has a handy ReadLine() method, and the writer has a WriteLine(), so you should need to work at the buffer/encoding level, but just with strings.


这篇关于在TCP客户端接收数据的格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆