如何以二进制格式发送Java对象并在C服务器中接收? [英] How to send a Java Object in binary format and receive in C server?

查看:120
本文介绍了如何以二进制格式发送Java对象并在C服务器中接收?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




  • 学生.java

      import java.io.Serializable; 
    公共类学生实现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;
    }

    }


  • Client.java

      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(例外e){
    e.printStackTrace();
    }
    clientSocket.close();
    }

    }




最后一个是我的server.c,它是c语言



/ *
C套接字服务器示例
* /

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


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

//创建套接字
socket_desc = socket(AF_INET,SOCK_STREAM,0);
if(socket_desc == -1)
{
printf(无法创建套接字);
}
puts(Socket created);

//准备sockaddr_in结构
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(7777);

//绑定
if(bind(socket_desc,(struct sockaddr *)& server,sizeof(server))< 0)
{
//打印错误消息
perror(绑定失败。错误);
返回1;
}
puts(bind done);

//听
listen(socket_desc,3);

//接受和传入连接
puts(等待传入连接...);
c = sizeof(struct sockaddr_in);

//接受来自传入客户端的连接
client_sock = accept(socket_desc,(struct sockaddr *)& client,(socklen_t *)& c);
if(client_sock< 0)
{
perror(接受失败);
返回1;
}
puts(接受连接);

//从客户收到消息
while((read_size = recv(client_sock,client_message,2000,0))> 0)
{
//将邮件发送回客户端
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);
}

返回0;
}

当我运行服务器和客户端时。我这样打算输出

  
sr
xp

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



<请提供您的建议或任何替代方案

解决方案

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



http://cbor.io/


I am having a Java object and now I want to convert it into binary form (binary protocol) and send across the network and a C client will receive it and print the contents. Like I have a student.java class

  • Student .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;
      }
    

    }

  • Client.java

      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();       
     }
    
    }
    

And the last is my server.c which is in c language

/* 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;
 }

when I am running the server and client. I am geeting output like this

 �
 sr
 xp

I want the output should be name and Id of the student printed on the server.

Please provide your suggestion or any alternate on the same

解决方案

Try CBOR: RFC 7049 Concise Binary Object Representation

http://cbor.io/

这篇关于如何以二进制格式发送Java对象并在C服务器中接收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆