Hello!
I have three classes: Server, Client and Cap. I'm trying to send Cap from Server to Client and back through sockets. Server implements runnable and in the run-method there is a infinite while-loop that look like this:
try { out.writeObject(new Cap()); out.flush(); Cap cap = (Cap) in.readObject(); boolean bol = false; if(cap.getValue()==2) { bol = true; } out.writeBoolean(bol); out.flush(); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Something is wrong"); break; } catch (ClassNotFoundException ex) {JOptionPane.showMessageDialog(null, "Something is wrong");} }
In the run-method but before the while-loop I did this:
out = new ObjectOutputStream(socket.getOutputStream()); in = new ObjectInputStream(socket.getInputStream());
In the Client-class I also have a while loop like this:
try { Cap cap = (Cap) in.readObject(); textBox.setText(cap.toString()); while(!JButtonHasBeendPressed) { } out.writeObject(cap); out.flush(); bol = in.readBoolean(); if(bol) { //Do something } else { //Do something else } JButtonHasBeendPressed = false; } catch (IOException ex) { //Do something } catch (ClassNotFoundException ex) { //Do something } }
Before the while-loop I did this:
When I'm debugging the Client-program it works just exactly as I want - the program gets inside "if(bol)". When I run the program, Server can send the object to Client, but after that something goes wrong. As you can see in the code I have a JButton in the Client-class, that is setting JButtonHasBeendPressed = true, but nothing happens when I'm pressing it. I get no error messages, literally nothing happens.out = new ObjectOutputStream(socket.getOutputStream()); in = new ObjectInputStream(socket.getInputStream());
Hank
--- Update ---
When I'm debugging both the server and the client, a IOException occurs at out.writeBoolean(bol); in the Server-class