小编典典

如何以二进制格式发送Java对象并在C服务器中接收?

java

我有一个Java对象,现在我想将其转换为二进制形式(二进制协议)并通过网络发送,C客户端将接收它并打印内容。就像我有一个学生班

学生.java

import java.io.Serializable;

public class Student implements Serializable {

    public String name;
    public int id;

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public int getId() { return id; }

    public void setId(int id) { this.id = id; }

}

客户端程序

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Client {

    public static void main(String[] args) throws IOException {

        Student s1 = new Student();
        s1.setId(10);
        s1.setName("abc");

        Socket clientSocket = new Socket("localhost", 7777);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

        try {
            FileOutputStream fos = new FileOutputStream("data.ser");
            ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
            oos.writeObject(s1);
            // oos.close();
            // fos.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        clientSocket.close();
    }

}

最后一个是server.c,它是用c语言编写的。

服务器

/*
 * C socket server example
 */

#include<stdio.h>
#include<string.h>    //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>    //write

int main(int argc , char *argv[]) {
    int socket_desc , client_sock , c , read_size;
    struct sockaddr_in server , client;
    char client_message[2000];

    // Create socket
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1) {
        printf("Could not create socket");
    }
    puts("Socket created");

    // Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 7777 );

    // Bind
    if (bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) {
        // Print the error message
        perror("bind failed. Error");
        return 1;
    }
    puts("bind done");

    // Listen
    listen(socket_desc , 3);

    // Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);

    // Accept connection from an incoming client
    client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
    if (client_sock < 0) {
        perror("accept failed");
        return 1;
    }
    puts("Connection accepted");

    // Receive a message from client
    while ((read_size = recv(client_sock , client_message , 2000 , 0)) > 0){
        // Send the message back to client
        printf("\n%s", client_message);
        write(client_sock , client_message , strlen(client_message));
    }

    if (read_size == 0) {
        puts("Client disconnected");
        fflush(stdout);
    }
    else if (read_size == -1) {
        perror("recv failed");
    }

    return 0;
}

当我运行服务器和客户端时。我得到这样的输出:

 �
 sr
 xp

我希望输出应该是服务器上打印的学生的姓名和ID。

请提供您的建议或其他建议。


阅读 286

收藏
2020-11-30

共1个答案

小编典典

尝试CBOR:RFC 7049简洁的二进制对象表示

http://cbor.io/

2020-11-30