C ++客户端到Java服务器(TCP / IP) [英] C++ Client to a Java Server(TCP/IP)
问题描述
一开始,我是非常新的与sockects和TCP / IP数据包的网络,如果你能以一个非常清楚的方式解释,我会很感激。我的机器人程序第一次尝试使用kinect与我们当前的机器人,但我们有一个问题。目前他们在java中编码,而他们计划在C ++编码kinect。是否可以使用TCP / IP数据包在两种语言之间发送信息?我们需要一个C ++客户端和一个Java服务器。
To start off, i'm very new to networking with sockects and TCP/IP packets, i would appreciate it if you could explain in a very clear manner. My robotics program is attempting to use a kinect with our current robot for the first time, but we have a problem. Currently they code in java, while they plan on coding the kinect in C++. Is it possible to use TCP/IP packets to send information between the two languages? We need a C++ Client and a Java server. If anyone has links or examples i would really appreciate it, thanks!
推荐答案
Java和C / C ++之间沟通的真正问题是字节排序问题。如果要发送二进制数据,则必须清楚和完整地设计通信结构,包括数字大小(字节),位顺序(lsb / msb,交换字节或非交换字节,短整型,整型和longs)和结构包装(结构中字段之间的填充字节数)。
The real problems communicating between Java and C/C++ is the byte ordering problem. If you are going to be sending binary data, you must design the communications structures clearly and completely, including number sizes (in bytes), bit-order (lsb/msb, swapped-byte or non-swapped bytes in short, ints, and longs), and structure packing (the number of pad bytes between fields in a structure).
我建议你不要以二进制通信,如果你可以避免它。有两个原因:
I recommend that you don't communicate in binary if you can avoid it. For two reasons;
- 不必担心位顺序,字节交换和结构打包。
- 通信无需解码数据。
EDIT
在C / C ++中,数据以使CPU访问快速和容易的方式存储。类/结构的字段在字边界上对齐(因为大多数CPU只能以全字块的方式访问存储器),并且这些位被排序以匹配CPU。但是,CPU可以具有不同的位顺序和字大小(16,32,64,...)。大多数英特尔CPU都是小端,大多数其他设计都是大端。为了使生活更有趣,java虚拟机在每个平台上都是大端。 http://en.wikipedia.org/wiki/Endianness
所以,如果你想要两个C / C ++机器能够通信,那么你必须以一种可以读取它的方式发送数据。通常,为了在异质环境中通信(称为通过电线),您指定所有通信都以特定格式完成。 TCP / IP使用MSB(最高有效位)排序。
So, if you want two C/C++ machines to be able to communicate, then you must send the data in a way that both can read it. Normally, to communicate in a heterogenous environment (called 'over the wire') you specify that all communications are done in a particular format. TCP/IP uses MSB (most significant bit) ordering. All the programs then have to translate (if necessary) from the wire format.
因为CPU消耗字长块中的内存,所以编译器把pad字节放在字段之间不填满整个字。对于读取32位字的机器,这样的结构:
Because CPUs consume memory in word length chunks, then the compilers put pad bytes between fields that don't fill a whole machine word. For a machine that reads 32 bit words, a structure like this:
struct example1 {
char someFlag;
int someCount;
};
实际上需要8个字节的内存。第一字段由单个字节的数据和3个填充字节组成,使得整数引用在字边界上对齐。如果一个简单的通信器尝试发送这个结构中的数据,例如 send(& example1,sizeof(example1));
到具有不同字大小或字节排序的另一个系统,其中另一个系统 read(& example1,sizeof(example1));
,那么example1.someCount的值可能与预期非常不同。
would actually take 8 bytes of memory. The first field consists of a single byte of data and 3 pad bytes, so that the integer references are aligned on word boundaries. If a naive communicator tries to send the data in this structure, say send(&example1, sizeof(example1));
to another system with a different word size or byte ordering, where the other system does read(&example1, sizeof(example1));
, then the value of example1.someCount may be very different than expected.
大多数通常是学术的,直到你把Java引入它。因为Java总是MSB格式。因此,即使在同一个硬件上,从C / C ++应用程序发送到Java应用程序也可能导致同样的意外结果。
Most of this is normally academic until you throw Java into it. Because Java is always MSB format. So sending from a C/C++ application to a Java application, even on the same hardware, may cause this same unexpected result.
Java包含我最喜欢的I / java.nio.ByteBuffer
。它有能力从几乎任何来源读int,longs,浮动和双打。如果你知道数据是如何创建的,这将读取它。 ByteBuffer有 getShort
, getInt
等方法获取任何类型,以及 order()
方法设置数据的字节顺序。
Java contains my favorite I/O class, java.nio.ByteBuffer
. It has the ability to read ints, longs, floats, and doubles from practically any source. If you know how the data was created, this will read it. ByteBuffer has getShort
, getInt
, etc. methods to get any type, as well as an order()
method to set the byte order of the data.
这篇关于C ++客户端到Java服务器(TCP / IP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!