从 UDP 套接字读取整个消息
我通过 UDP
套接字发送数据,并使用 read()
在循环中接收数据。 输入数据如下所示:
String 1
String 2
String 3
....
我使用 send()
写出数据,每个字符串一个接一个(在循环中)。 如何确保能够以正确的方式重建接收端的数据(当我放入字符串时)?
接收到的数据可以在行中间的任何位置分割,如下所示:
Packet 0: Stri
Packet 1: ng 1
Packet 2: String 2 St
Packet 3: ring 3
...
我是否必须引入自定义的END OF MESSAGE
字节序列来告诉?因为 EOF 在这里没有帮助。
我需要能够判断包是否已损坏,以及属于一起的数据块的开始和结束位置,因为我将它们以 S
开头并以 Number
结尾代码>!我无法使用 TCP
因为我需要广播/多播支持。
I'm sending data over an UDP
socket and receive it in a loop with read()
.
The input data looks like this:
String 1
String 2
String 3
....
I write data out with send()
, each string after each other (in a loop).
How do I make sure that I can reconstruct the data on the receive end in the correct fashion (as I put the strings in)?
The received data can be split anywhere in the middle of the lines like so:
Packet 0: Stri
Packet 1: ng 1
Packet 2: String 2 St
Packet 3: ring 3
...
Do i have to introduce a custom END OF MESSAGE
byte sequence to tell? Because EOF
won't help here.
I need to be able to tell if a package is corrupted, and where the data blocks that belong together begin and end, as I sent them away beginning with S
and ending with the Number
! I can't use TCP
because i need broadcast/multicast support.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望所有消息都按照发送的顺序到达,并且有“消息结束”指示,也许 TCP 更好:-)
(TCP 开箱即用。)
If you want all messages to arrive, and in the same order they were sent, and to have an "end of message" indication, maybe TCP is better :-)
(TCP does this all out of the box.)