简体   繁体   English

套接字Java将对象发送到C ++服务器

[英]Sockets java sending object to c++ server

I am using sockets and I have one c++ server and one java client. 我正在使用套接字,并且有一台C ++服务器和一台Java客户端。

My c++ server is waiting for a c++ struct like this one: 我的c ++服务器正在等待像这样的c ++结构:

struct Testing{

    double  a;
    double  b;
    double  c;

};

And my server code looks like this: 我的服务器代码如下所示:

int recvStruct(int fd)
{
    struct Testing test;
    int ibytes = sizeof(struct Testing);

    if (ibytes!=read(fd,&test,ibytes))
    {
        printf("Error read\r\n");
        return 0;
    }
    else
    {
        dosomething();

Then in my java client, i created one class: 然后在我的Java客户端中,我创建了一个类:

public class Testing{
    double a,b,c;

public Testing(double a, double b, double c){
    this.a = a;
    this.b = b;
    this.c = c;
    }
}

and for the last, my client java code is this one: 最后,我的客户端Java代码是以下代码:

try {
        Socket socket = new Socket("ipaddress", 12345);

        OutputStream os= socket.getOutputStream();
        Testing object = new Object(1,2,3);
        os.writeObject(object);
        socket.close();

 } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        /* e.printStackTrace();
        response = "UnknownHostException: " + e.toString();*/
 } catch (IOException e) {
        // TODO Auto-generated catch block
        /*e.printStackTrace();
        response = "IOException: " + e.toString();*/
 } finally {
        if (socket != null) {
             try {
                 socket.close();
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                /* e.printStackTrace();*/
             }
 }

My problem is that i always get that error read message. 我的问题是我总是收到该错误读取消息。

Thank you. 谢谢。

Your code just assumes that your C++ Testing class consists of the exact same number of bytes, in the exact same order, as the result of Java's OutputStream.writeObject function. 您的代码仅假定 C ++ Testing类由Java的OutputStream.writeObject函数的结果按完全相同的顺序由完全相同的字节数组成。

OutputStream.writeObject isn't even a function to serialise the component bytes of its members; OutputStream.writeObject甚至都不是序列化其成员的组件字节的函数。 it does much more than that : 它的作用远不止于此

The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are written. 写入对象的类,类的签名以及该类及其所有超类型的非瞬态和非静态字段的值。

In conclusion, given that there is no relationship between the two whatsoever, success is extremely unlikely. 总之,鉴于两者之间没有任何关系,成功是极不可能的。

Instead, serialise to your own known format, then deserialise into the language structure you require. 相反, 还原序列化到自己已知的格式,然后deserialise到您所需要的语言结构。 Research the bolded terms for more information. 研究粗体字词以获取更多信息。

If your struct is a sequence of 3 double, assuming in C++ a double is 8 byte as in Java, you should send a sequence of 24 bytes. 如果您的结构是3个double的序列,那么在C ++中,如Java中一样,假设double是8字节,则应发送24个字节的序列。 To convert each double I would use: 要转换每个双精度数,我将使用:

long bits = Double.doubleToLongBits(someDouble);
byte[] bytes = new byte[8];
for(int offset = 0, shift = 7*8; 
        offset < bytes.length; 
        offset++, shift-=8)
        bytes[offset] = (byte)(bits >>> shift);

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

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