在套接字上读和写对象,java

问题描述 投票:0回答:1

我试图在客户端和服务器之间写和读一个对象,但是当我在客户端的输出中收到对象时,我收到了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;
  }
}

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

祝你愉快

java sockets serversocket
1个回答
1
投票

我想你的客户端程序在向服务器发送坐标后就结束了。服务器在收到坐标后,试图再发送一个坐标给客户端,但客户端已经关闭了连接。

服务器代码:

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

和客户端代码。

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

我加了一行 connectionManager.readCoordinates(); 发出线到服务器后,等待响应。这一行等待响应,而不是在发送线到服务器后立即关闭连接。


0
投票

我找到了答案。classpackage 需要相同,然后你需要设置相同的 serialVersionUID.

© www.soinside.com 2019 - 2024. All rights reserved.