Java: client-sever app.- WHILE on ObjectInputStream read thread?

If you change while to if in your run() method you read only one object and exit. If your client-server protocol is very simple, e.g. They send only one object per session both versions are equal. Otherwise the system will not function properly: one all other objects will not arrive.

Up vote 0 down vote favorite share g+ share fb share tw.

This must be very easy but I am obviously missing some basic understanding. I have simple client server application server: public class GameServer{ private Socket clientSocket; private ServerSocket serverSocket; private ArrayList oosPlayers=new ArrayList(); public static void main(String args){ GameServer server = new GameServer(); server.startGame(); } public void startGame(){ try{ serverSocket = new ServerSocket(10008); System.out. Println("Server started at:" + serverSocket); while(true){ System.out.

Println("Server: Waiting for client requests..."); clientSocket=serverSocket.accept(); System.out. Println("Server: Client connection accepted at:"+clientSocket); //remember player's output stream oosPlayers. Add(new ObjectOutputStream(clientSocket.getOutputStream())); //read data from clients Thread receiverThread = new Thread(new DataReceiver()); receiverThread.start(); } } catch(IOException ex){ System.out.

Println(ex.getMessage()); } } public class DataReceiver implements Runnable{ public void run(){ try{ String mess; ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream()); while((mess=(String)ois.readObject())! = null){ System.out. Println("Server: "+mess); send(); } } catch(Exception ex){ System.out.

Println(ex.getMessage()); } } public void send(){ for (ObjectOutputStream oos : oosPlayers){ try{ oos. WriteObject(new String("There is "+oosPlayers.size()+" player(s) at the board")); oos.flush(); } catch(Exception ex){ System.out. Println(ex.getMessage()); } } } } } client: public class GameClient implements ActionListener{ private Socket socket; private ObjectInputStream ois; private ObjectOutputStream oos; private JButton button; public static void main(String args){ GameClient client = new GameClient(); client.drawBoard(); } public void drawBoard(){ JFrame frame = new JFrame("A game"); frame.

SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); button = new JButton("Send"); button. AddActionListener(this); configureConnection(); Thread receiverThread = new Thread(new DataReceiver()); receiverThread.start(); frame.getContentPane().

Add(new JPanel(). Add(button)); frame. SetSize(200,200); frame.

SetVisible(true); } public void configureConnection(){ try{ socket = new Socket("127.0.0.1", 10008); ois = new ObjectInputStream(socket.getInputStream()); oos = new ObjectOutputStream(socket.getOutputStream()); } catch(Exception ex) { System.out. Println(ex.getMessage()); } } public void actionPerformed(ActionEvent ev){ if (ev.getSource()==button){ try{ oos. WriteObject(new String("Some data from client")); oos.flush(); } catch(Exception ex){ System.out.

Println(ex.getMessage()); } } } public class DataReceiver implements Runnable{ public void run(){ try{ String mess; while((mess=(String)ois.readObject())! = null){ System.out. Println("Client:"+mess); } } catch(Exception ex){ System.out.

Println(ex.getMessage()); } } } } My question relates to receiverThread on both sides. When I use WHILE during read from ObjectInputStream (in RUN method), sever listens to clients requests and properly dispatches them to all ObjectOutputStream s. If I change this WHILE to IF data are read (and send) from server only once per each client.

The same applies to client's WHILE/IF. My understanding is that when I run receiverThread it somehow stops the thread at WHILE and keeps reading from input stream, whereas using IF let's the thread to finish (and thereby stops read process). How is that possible that although WHILE condition is not satisfied i.e.

Initially (no client got connected yet) it keeps receiverThread alive. The same happens after receiverThread already read data and there is nothing left in the stream. Would appreciate some explanation on this basic issue.

Regards Marcin java multithreading objectinputstream link|improve this question asked Nov 3 '11 at 15:01user102788811.

Thanks for the answer. I am still curious how is that possible that first time while((mess=(String)ois.readObject())! = null) is executed at server, the condition fails (stream is empty), but it doesn't exit run method and keeps listening on input stream.

– user1027888 Nov 3 '11 at 15:24 It does not keep listening InputStream. It is listening socket because you have not closed it. Clear implementation should wrap while loop with try/finally block and close the stream in finally block.

– AlexR Nov 3 '11 at 15:30 This might not be correct, but I believe it doesn't exit the while((mess=(String)ois.readObject())! = null), because your socket is in a blocking mode, and the reading thread will block on the readObject() method until either it reads data, or socket is closed (which causes Exception to be thrown). – Sam Goldberg Nov 3 '11 at 16:06 Well, literally I would read this as "while there is something to be read from the input stream (as socket is still open) do something" but this would mean "if there is nothing there left, leave the loop".

According to your suggestion this should be rather interpreted as "while socket is in blocking mode, keep reading from it", whereas using IF this would go as "if socket is in blocking mode, read from it". Maybe the confusion seats here, but for this moment I am satisfied with this explanation and will assume it is so :) thanks – user1027888 Nov 3 '11 at 20:28 Off topic: is there any way I can appreciate these answers – user1027888 Nov 3 '11 at 20:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions