【问题标题】:Read and Write object on a socket java在套接字 java 上读取和写入对象
【发布时间】:2020-08-21 16:21:33
【问题描述】:

我正在尝试在客户端和服务器之间写入和读取对象,但是当我在客户端的输出中收到对象时,我收到 server.Coordinates,然后服务器崩溃。

调试发现这个功能不起作用,但是坐标类是一样的:

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

这是服务器:

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

这是客户:

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

“坐标”类是一样的:

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

我哪里出错了?我也不能从服务器写到客户端。

祝你有美好的一天

【问题讨论】:

  • 请显示服务器的准确输出。
  • @Code-Apprentice ServerSocket awaiting connections... Connection from Socket[addr=/127.0.0.1,port=61595,localport=8080]! client.Coordinates Error Java Result: 1 BUILD SUCCESSFUL (total time: 6 seconds) 我发现在服务器中,当我到达 objectInputStream 行时,变量为空,服务器崩溃,但在客户端 objectInputStream 工作
  • @Code-Apprentice 我现在正在调试,我发现问题出在从套接字读取对象的指令上
  • 导致问题的坐标是什么?
  • @NomadMaker 我尝试了很多组合,但读取功能从未起作用。我认为问题出在演员或课堂上,因为我在套接字上写了一个对象,客户端没有停下来。仅当调用该函数时客户端或服务器崩溃。

标签: java sockets serversocket


【解决方案1】:

我认为您的客户端程序在将坐标发送到服务器后结束。接收线后服务器尝试向客户端发送另一个,但客户端已经关闭连接。

服务器代码:

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

和客户端代码:

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

我在将绳索发送到服务器后添加了 1 行 connectionManager.readCoordinates();。此行等待响应,在将线发送到服务器后不会立即关闭连接。

【讨论】:

    【解决方案2】:

    我找到了答案。 使用套接字传递对象,classpackage 需要相同,然后您需要设置相同的serialVersionUID

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      相关资源
      最近更新 更多