简体   繁体   English

处理字符串的正确方法是什么:Java客户端和C ++服务器

[英]What is the proper way to handle strings : Java client & C++ server

I'm writing a C++ server/client application (TCP) that is working fine but I will soon have to write a Java client which obviously has to be compatible with the C++ server it connects to. 我正在编写一个工作正常的C ++服务器/客户端应用程序(TCP),但我很快就要编写一个Java客户端,它显然必须与它连接的C ++服务器兼容。

As for now, when the server or client receives strings (text), it loops through the bits till a '\\0' is found, which marks the end of the string ... 至于现在,当服务器或客户端接收字符串(文本)时,它会循环遍历位,直到找到'\\ 0',这标志着字符串的结束......

Here's the question : is it still a good practice to handle strings that way when communicating over Java/C++ rather than C++/C++ ? 这是一个问题:在通过Java / C ++而不是C ++ / C ++进行通信时,以这种方式处理字符串仍然是一个好习惯吗?

There's one thing you should read about: Encodings. 你应该阅读一件事:编码。 Basically, the same sequence of bytes can be interpreted in different ways. 基本上,可以以不同方式解释相同的字节序列。 As long as you pass things around in C++ or Java, things will agree on their meaning, but when using the net (ie a byte stream) you must make up your mind. 只要你用C ++或Java传递东西,事情会就它们的含义达成一致,但是当使用网络(即字节流)时,你必须下定决心。 If in doubt, read about and use UTF-8. 如有疑问,请阅读并使用UTF-8。

考虑使用Protocol BuffersThrift而不是滚动自己的协议。

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

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