简体   繁体   English

在插槽 java 上读写 object

[英]Read and Write object on a socket java

i'm trying to write and read an object between a client and a server, but when i receive the object in the output of the client i receive server.Coordinates, and then the server crash.我正在尝试在客户端和服务器之间写入和读取 object,但是当我在客户端的 output 中收到 object 时,我收到了服务器崩溃。

With the debug i found that this function doesen't work, but the class of coordinates is the same:通过调试,我发现这个 function 不起作用,但是坐标的 class 是相同的:

void readCoordinates() throws IOException, ClassNotFoundException {
        //creazione e ricezione oggetto coordinate 
            Coordinates coordinates = (Coordinates) objectInputStream.readObject();
            System.out.println("" + coordinates.getX() + " - " + coordinates.getY());
}    

This is the server:这是服务器:

    public class Server {

      static Server server;

      //socket server
      ServerSocket serverSocket;

      //stream output
      OutputStream outputStream; 
      ObjectOutputStream objectOutputStream;
      //stream input
      InputStream inputStream ;
      ObjectInputStream objectInputStream;

      public static void main (String[] args) { 

        //Server start
        server = new Server(); 
        server.start(); 
      } 

      public void start(){ 
        try {
            //creazione server socket
            serverSocket = new ServerSocket(8080); 
            System.out.println("ServerSocket awaiting connections...");

            //accept connection
            Socket socket = serverSocket.accept();
            System.out.println("Connection from " + socket + "!");

            //output
            outputStream = socket.getOutputStream();
            objectOutputStream = new ObjectOutputStream(outputStream);

            //input
            inputStream = socket.getInputStream();
            objectInputStream = new ObjectInputStream(inputStream);

            //receive coordinates
            readCoordinates();

            //write coordinates 
            writeCoordinates(1, 1);

        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("Error");

            System.exit(1);
        }
      } 

      void readCoordinates() throws IOException, ClassNotFoundException {
        //creazione e ricezione oggetto coordinate 
            Coordinates coordinates = (Coordinates) objectInputStream.readObject();
            System.out.println("" + coordinates.getX() + " - " + coordinates.getY());
      }

      public void writeCoordinates(int x, int y) throws IOException{
        //creazione e invio oggetto coordinate 
        Coordinates coordinates = new Coordinates(x, y);
        objectOutputStream.writeObject(coordinates);
      }
    }

This is the client:这是客户端:

public class ConnectionManager {

  //server
  String serverIP = "127.0.0.1";                  
  int serverPort = 8080;

  Socket socket;      
  // stream output
  OutputStream outputStream; 
  ObjectOutputStream objectOutputStream;
  // stream input
  InputStream inputStream;
  ObjectInputStream objectInputStream;

  public ConnectionManager() {
    try { 
        //creation socket  
        System.out.println("Socket creation...");
        this.socket = new Socket(serverIP,serverPort);

        //creation input e output stream
        System.out.println("Creation input and output stream...");
        this.inputStream = this.socket.getInputStream();
        this.objectInputStream = new ObjectInputStream(this.inputStream);
        this.outputStream = socket.getOutputStream();
        this.objectOutputStream = new ObjectOutputStream(this.outputStream);


    } catch (UnknownHostException e){
        System.err.println("Unreacheable host"); 
    } catch (IOException e) {
        System.out.println(e.getMessage());
        System.out.println("Error");

        System.exit(1);
    }
  }

  public void writeCoordinates(int x, int y) throws IOException{
    //send coordinates
    Coordinates coordinates = new Coordinates(x, y);
    objectOutputStream.writeObject(coordinates);
  }

  void readCoordinates() throws IOException, ClassNotFoundException {
    //read coordinates
    Coordinates coordinates = (Coordinates) objectInputStream.readObject();
    System.out.println("" + coordinates.getX() + " - " + coordinates.getY());
  }
}

The class "Coordinates" is the same: class“坐标”是一样的:

public class Coordinates implements Serializable{

  //coordinate
  int x;
  int y;

  Coordinates(int x, int y) {
    this.x = x;
    this.y = y;
  }

  public int getX() {
    return x;
  }

  public int getY() {
    return y;
  }
}

Where i'm going wrong?我哪里错了? I can't write from the server to the client too.我也无法从服务器写入客户端。

Have a nice day祝你今天过得愉快

I think your client program ends after send coords to server.我认为您的客户端程序在将坐标发送到服务器后结束。 Server after recive cords try to send another to client but client already close connecttion.接收线后服务器尝试向客户端发送另一个,但客户端已经关闭连接。

Server code:服务器代码:

Server server = new Server();
server.start();

And client code:和客户端代码:

ConnectionManager connectionManager = new ConnectionManager();
connectionManager.writeCoordinates(5,5);
connectionManager.readCoordinates();

I add 1 line connectionManager.readCoordinates();我添加 1 行connectionManager.readCoordinates(); after sending cord to server.将线发送到服务器后。 This line wait for response and not closing connection immediately after sending cords to server.此行等待响应并且在将线发送到服务器后不会立即关闭连接。

I found the answer.我找到了答案。 For pass an object with a socket, the class and the package needs to be the same, and then you need to set the same serialVersionUID .要通过带插座的 object, classpackage需要相同,然后您需要设置相同的serialVersionUID

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

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