简体   繁体   English

Java客户端通过TCP连接与C ++服务器通信

[英]java client talking to c++ server through TCP connection

I'm new to networking so hopefully I will be able to explain my issue. 我是网络新手,所以希望我能够解释我的问题。

My boss recently made a dummy server for me to test my application's networking and I ran into issue regarding java to c++. 我的老板最近为我制作了一个虚拟服务器来测试我的应用程序的网络,我遇到了从Java到C ++的问题。 His server was written in c++ and when run, it waits and listens for a particular c++ data struct named "RemoteMsgStruct" to be passed: 他的服务器是用c ++编写的,运行时它等待并侦听名为“ RemoteMsgStruct”的特定c ++数据结构:

typedef struct
{
unsigned int Length;
unsigned int BoardType;
unsigned int MsgType;
unsigned char Msg[MAX_MSG_SIZE];
} RemoteMsgStruct;

The problems lies in the fact that my program is written in java. 问题在于我的程序是用Java编写的。 How would I go about passing a similar message through java for the server to process? 我将如何通过Java传递类似的消息以供服务器处理?

Thanks ahead of time for the help. 提前感谢您的帮助。 If anything isn't explained well enough or appears to be missing let me know. 如果没有充分解释或遗漏,请告诉我。 Like I said, I'm new to both networking and stackoverflow :) 就像我说的,我是网络和stackoverflow的新手:)

You can pass exactly the data expected by your boss's test program, without asking your boss to modify it. 您可以准确传递老板测试程序所期望的数据,而无需您的老板对其进行修改。

On the Java side, you can write data in the layout defined by the C++ struct via several methods, including: * java.io.DataOutputStream * java.nio.ByteBuffer and java.nio.channels.SocketChannel. 在Java方面,您可以通过几种方法在C ++结构定义的布局中写入数据,包括:* java.io.DataOutputStream * java.nio.ByteBuffer和java.nio.channels.SocketChannel。

You may encounter issues of field alignment and byte order. 您可能会遇到字段对齐和字节顺序的问题。 * Some fields in a C struct may start after padding to align with word boundaries. * C结构中的某些字段可能在填充后开始以与单词边界对齐。 * Java will write network byte order (big-endian) by default. *默认情况下,Java将写入网络字节顺序(big-endian)。 On the Java side, ByteBuffer allows choice of a different order. 在Java方面,ByteBuffer允许选择其他顺序。

Welcome to SO. 欢迎来到SO。

As to your question, there's a reason why messaging formats like XML and JSON are so popular, they solve problems like this. 关于您的问题,有一个原因使得XML和JSON之类的消息传递格式如此流行,它们可以解决此类问题。 As it is you are going to have to battle with sending over unsigned ints and worrying about character encoding and endian-ness. 这样一来,您将不得不与发送未签名的int并担心字符编码和字节序有关。 If you cannot change the format on the server side then I will recommend you take a look at ByteBuffer . 如果您不能在服务器端更改格式,那么我建议您看一下ByteBuffer You will still have to do some massaging but at least it will relieve some of the pain. 您仍然需要做一些按摩,但是至少可以减轻一些痛苦。

Andy's answer is correct. 安迪的答案是正确的。 However it should be said that your boss's program needs to be rewritten. 但是应该说您老板的程序需要重写。 Passing C structs directly over a network is never a good idea even when the other end is written in C. That technique relies on the following being the same at both ends: 即使另一端用C编写,直接通过网络传递C结构也绝不是一个好主意。该技术依赖于两端的以下相同之处:

  1. The hardware byte order 硬件字节顺序
  2. The compiler's filling and packing rules 编译器的填充和打包规则
  3. The surrounding #pragmas 周围的#pragmas

and therefore 因此

  1. The compiler vendor 编译器供应商
  2. The compiler version 编译器版本

This (incomplete) list is far too long already, and we are still only considering C to C. 这个(不完整的)列表已经太长了,我们仍然只考虑C到C。

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

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