读取套接字有线问题 - 在消息中添加一些有线字符
例如,当我发送“1234”时,使用 SFML 库的 C++ 服务器读取“1234╠╠╠╠╤lσ╝╝√=¡↑עσ”。从 C++ SFML 客户端发送到服务器时一切正常。原始客户端是用 Onj-c 编写的,带有异步套接字库。搞什么? (所有 TCP)
When I'm sending, for example, "1234", the server, which is C++ using SFML library reads "1234╠╠╠╠╤lσ╝╝√=♦↑עσ". When sending from C++ SFML clinet to the server it's all OK. The original client written in Onj-c with Async Socket library. WTF? (All TCP)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定这对您有帮助,但我建议发送类似“1234\0”的内容。
I don't sure that this will help you, but I suggest to send something like this "1234\0".
听起来服务器假设它接收到的数据是以空终止的,但其中一个客户端只是发送“1234”而不是“1234\0”。因此,您看到的奇怪字符可能是服务器内存的该部分中发生的任何垃圾。
您可以使用 Wireshark 之类的工具来准确查看网络上发生的情况,以及两者之间的区别客户。
It sounds like the server is assuming that the data it receives is null-terminated, but one of the clients is just sending "1234" instead of "1234\0". So the weird characters you're seeing might be whatever garbage happened to be in that part of the server's memory.
You can use a tool like Wireshark to see exactly what's going out over the wire, and how it differs between the two clients.
SFML 中的简单实现确实假设一个以 null 结尾的字段 - 并将向您显示其原始缓冲区。这确实没有终止。要么用 \0 终止发送字符串,要么发送类似 12345 的字符串 - 并更改打印以在 [len] 处放置 \0 - 或类似的。
A naive implementation in SFML does indeed assume a null-terminated field - and will show you its raw buffer. Which does indeed not terminate. Either terminate the sending string witha \0 or alternatively sent something like 12345 - and change the print to put a \0 at [len] - or similarly.
从库中获取字符串以及数据长度,并在 [len] 处添加 \0
Get the string from your lib together with the lenght of the data and add \0 at [len]