user3548416
user3548416

Reputation: 117

ObjectOutputStream - client/server communication

I am new to java client server communications and I'm confronted with sending an object from client side to server side. Somehow my programm stucks while trying to transfer an object. The likely reason might be the ObjectOutputStream implementation.

Database login button

class Handler implements ActionListener {
   Client client = new Client();
   List<String> parameterList = new ArrayList<String>();
   ClientParameter parameter;

   public void actionPerformed(ActionEvent event) {
      if(event.getSource()==buttonConnect) {
         parameterList.add(username);
         parameterList.add(password);
         parameter = new ClientParameter(parameterList);
         client.connectToServer(parameter);
}

Object ClientParameter

public class ClientParameter {
   List<String> parameterList;

   public ClientParameter(List<String> parameterList) {
      this.parameterList = parameterList;
   }

   public List<String> getParameterList() {
      return parameterList;
   }
   public void setParameterList(List<String> parameterList) {
      this.parameterList = parameterList;
   }
}

Class client

public void connectToServer(ClientParameter parameter) {
   // Verbindung mit dem Server herstellen
   Socket server = null;
   try {
      server = new Socket("localhost", 3141);
      // programm hangs while reading this 2 lines..
      ObjectInputStream objectIn = new ObjectInputStream(server.getInputStream());
      ObjectOutputStream objectOut = new ObjectOutputStream(server.getOutputStream());

      objectOut.writeObject(parameter);
   } catch(UnknownHostException e) {
      e.printStackTrace();
   } catch(IOException e) {
      e.printStackTrace();
   } finally {
      if(server!=null) {
         try {
            server.close();
         } catch(IOException e) {
            System.out.println(e);
         }
      }
   }
}

And the server class

public class Server {
   private static void handleConnection(Socket client) throws IOException {
      ObjectInputStream objectIn = new ObjectInputStream(client.getInputStream());
      ObjectOutputStream objectOut = new ObjectOutputStream(client.getOutputStream());

      ClientParameter parameter;
      try {
         parameter = (ClientParameter) objectIn.readObject();
         System.out.println(parameter);
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) throws IOException {
      ServerSocket server = new ServerSocket(3141);
      while(true) {
         Socket client = null;
         try {
            client = server.accept();
            handleConnection(client);
         }
         catch(IOException e) {
            e.printStackTrace();
         }
         finally {
            if(client!=null) {
               try {
                  client.close();
               }
               catch(IOException e) {
                  System.out.println(e);
               }
            }
         }
      }
   }
}

Thank you!

Upvotes: 0

Views: 700

Answers (1)

JB Nizet
JB Nizet

Reputation: 692281

The server starts by reading what the client sends. And the client also starts by reading what the server sends. So it's a deadlock.

Indeed, as documented, the constructor of ObjectInputStream blocks until it has received the serialization stream header:

A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

Remove the following line, since you don't do anything with the ObjectInputStream anyway:

ObjectInputStream objectIn = new ObjectInputStream(server.getInputStream());

Upvotes: 3

Related Questions