0

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.

With the debug i found that this function doesen't work, but the class of coordinates is the same :

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 :

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

7
  • Please show the exact output from the server. Commented May 6, 2020 at 16:02
  • @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) I find that in the server when i reach the line of objectInputStream, the variable is empty and the server crash, but on the client objectInputStream works Commented May 6, 2020 at 16:14
  • @Code-Apprentice i'm now debugging, and i found that the problem is on the instruction that read the object from the socket Commented May 6, 2020 at 16:45
  • And what was the coordinate that caused the problem? Commented May 6, 2020 at 16:59
  • @NomadMaker i tried many combination, but the reading function has never worked.I think the problem is in the cast or in the class, because where i write an object on the socket, the client doesn't stop. Only when the function is called the client or sever crash. Commented May 6, 2020 at 17:08

2 Answers 2

1

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(); after sending cord to server. This line wait for response and not closing connection immediately after sending cords to server.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.