从 TCPClient 读取时出现问题

发布于 2024-11-11 17:48:42 字数 546 浏览 0 评论 0原文

我正在用 C# 制作一个简单的客户端应用程序,但遇到了问题。

服务器应用程序发送一个格式为 "" 等的字符串。换句话说,第一个符号是整数,其余的都是任意的,全部都以一个空格分隔。

在读取这个字符串时,我遇到的问题是我的程序首先读取带有 的字符串,然后下次读取时我会得到消息的其余部分。 例如,如果我要对收到的内容进行写行,它将如下所示: (如果他发送“1 0 0 0”)

1
 0 0 0 

(编辑:格式似乎不允许这样做。1 位于其自己的一行上,其余的应该位于下面的行上,包括 之前的空格第一个 0)

我已经没有办法解决这个问题了。这是方法(我注释掉了我尝试过的一些东西): http://pastebin.com/0bXC9J2f

编辑(再次):我忘了,当我正在调试,只是一步一步地完成所有事情,所以我无法通过这种方式找到问题的根源。

I'm making a simple client application in C#, and have reached a problem.

The server application sends a string in the format of "<number> <param> <param>" etc. In other words, the first symbol is an integer, and the rest are whatever, all are separated by one space each.

The problem I get, when reading this string, is that my program first reads a string with the , and then the next time I read I get the rest of the message.
For example, if I were to do a writeline on what I receive, it would look like this:
(if he sends "1 0 0 0")

1
 0 0 0 

(EDIT: The formatting doesn't seem to permit this. The 1 is on a row of its own, the rest are supposed to be on the row below, including the space preceding the first 0)

I've run out of ideas how to fix this. Here's the method (I commented out some stuff I tried):
http://pastebin.com/0bXC9J2f

EDIT (again): I forgot, it seems to work just fine when I'm in debug and just go through everything step by step, so I can't find any source of the problem that way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘然心甜 2024-11-18 17:48:42

TCP 是基于流的,而不是基于消息的。一条 Read 可以包含以下任何一种选择:

  • 消息的一小部分
  • 半条消息
  • 恰好一条消息
  • 一条半消息
  • 两条消息

因此,您需要使用某种方法来查看是否完整消息已到达。最常见的方法是:

  • 添加表示消息结束的页脚(例如空行)
  • 添加包含消息长度的固定长度标头

TCP is stream based and not message based. One Read can contain any of the following alternatives:

  • A teeny weeny part of message
  • A half message
  • Excactly one message
  • One and a half message
  • Two messages

Thus you need to use some kind of method to see if a complete message have arrived. The most common methods are:

  • Add a footer (for instance an empty line) which indicates end of message
  • Add a fixed length header containing the length of the message
追星践月 2024-11-18 17:48:42

如果您的协议是直接 TCP,那么您无法发送消息、字符串或除八位字节(字节)流之外的任何其他内容。你的“字符串”末尾有空吗?如果是这样,您需要附加接收到的数据,直到 null 到达,然后您就得到了消息。

如果这是您的问题,那么您应该对协议进行编码,以便无论在套接字上进行多少次读取调用,它都可以正常工作,例如。如果服务器发送 [99 数据字节+#0] 的空终止字符串,如果一次调用返回 100 个字节,100 次调用接收 1 个字节,或者任何以下情况,您的协议应该能够组装正确的字符串:之间。

平均值,
马丁

If your protocol is straight TCP, then you cannot send messages, strings or anything else except octet, (byte) streams. Does your 'string' have a null at the end? If so, you need to append received data until the null arrives, then you have your message.

If this is your problem, then you should code your protocol so that it works no matter how many read calls are made on the socket, eg. if a null-terminated string of [99 data bytes+#0] is sent by the server, your protocol should be able to assemble the correct string if 100 bytes are returned in one call, 1 byte is received in 100 calls, or anything in between.

Rgds,
Martin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文