Using ObjectInputStream and ObjectOutputStream between client and server process

10,518

You must call flush() on the stream on the client side to actually send the data (if the socket's buffer is not full). You see your program hanging because the client does not send the data, and the server is blocking, waiting for the data that will never come.

Share:
10,518
sahar
Author by

sahar

Updated on June 05, 2022

Comments

  • sahar
    sahar almost 2 years

    I develop a client-server java application and I used ObjectOutputStream and ObjectInputStream to send and receive data between client and server process. I need to send Array or object or primitive data but the problem appears when I use ObjectOutputStream and ObjectInputStream to send and receive primitive values ( writeDouble(), readDouble(), writeUTF(), readUTF() ) . the program suspended and stopped working. why, what is the problem?

    these are a fragments of my program

    // client program
    
        ObjectOutputStream toServer;
        ObjectInputStream fromServer;
    
    // Establish connection with the server
                Socket socket = new Socket(host, 7000);
    
                // Create an output stream to the server
                toServer = new ObjectOutputStream(socket.getOutputStream());
                fromServer = new ObjectInputStream(socket.getInputStream());
    
    double num1 = Double.parseDouble(jtf1.getText().trim());
                        double num2 = Double.parseDouble(jtf2.getText().trim());
                        try {
                            toServer.writeUTF("multiply");
                            toServer.writeDouble(num1);
                            toServer.writeDouble(num2);
                            double result = fromServer.readDouble();
                            res.setText(String.valueOf(result));
                        } catch (IOException ex) {
                            System.err.println(ex);
                        }
    
    // server program
    
    private ObjectOutputStream outputToClient;
        private ObjectInputStream inputFromClient;
    
    // Create a server socket
                ServerSocket serverSocket = new ServerSocket(7000);
    
    while (true) {
                    // Listen for a new connection request
                    Socket socket = serverSocket.accept();
                    System.out.println("connect ");
                    outputToClient = new ObjectOutputStream(socket.getOutputStream());
                    // Create an input stream from the socket
                    inputFromClient =
                            new ObjectInputStream(socket.getInputStream());
                    while(true)  {
                    // Read from input
                    String command = inputFromClient.readUTF();
                    System.out.println("receive command");
                    checkRequest(command);
                    }
                    // Write to the file
                    //outputToFile.writeObject(object);
    
                }
    
    public void checkRequest(String cmd){
            //Object o = null;
            try{
            if(cmd.equals(MULTIBLY)){
                double x = inputFromClient.readDouble();
                double y = inputFromClient.readDouble();
                double result = x*y;
                outputToClient.writeDouble(result);
                System.out.println("send result");
            }else if (cmd.equals(DIVIDE)){
                int x = inputFromClient.readInt();
                int result = 1000/x;
                outputToClient.writeDouble(result);
            }
            } catch(IOException io){
    
            }
        }
    

    when I change ObjectOutputstream and ObjectInputStream to DataOutputStream and DataInputStream every thing goes correctly !