Only create the ObjectInputStream once (outside the loop) for a client connection, then put the readObject method into the loop.
Up vote 0 down vote favorite share g+ share fb share tw.
Ok so , I have a thread class called 'Client' every time the server accepts a connection it creates a new Client....The run method listens for messages from the client and I am useing ObjectInputStream .. do { ObjectInputStream in = null; try { in = new ObjectInputStream(socket.getInputStream()); String message = (String) in.readObject(); System.out. Println(message); } catch (ClassNotFoundException ex) { isConnected = false; System.out. Println("Progoramming Error"); } catch (IOException ex) { isConnected = false; System.out.
Println("Server ShutDown"); System. Exit(0); } } while(isConnected); The Problem I have is that why do I have to create a new ObjectInputStream every time it loops...and if I close the input stream at the end of the loop and it loops again for another message I will get a error...Please some one help java sockets objectinputstream link|improve this question asked Aug 11 '11 at 7:04Mohammad Mustafa Hossaini62.
2 You don't have to, and you shouldn't. – JB Nizet Aug 11 '11 at 7:16 And getting a ClassNotFoundException is neither an indication that you are no longer connected nor a programming error. It indicates a deployment error.
– EJP Aug 11 '11 at 8:31 Could you past what errors are you getting and it would also help if you posted the client code as well. – beny23 Aug 12 '11 at 12:04.
Only create the ObjectInputStream once (outside the loop) for a client connection, then put the readObject method into the loop. Here's a working test class: public class TestPrg { public static void main(String... args) throws IOException { ServerListener server = new ServerListener(); server.start(); Socket socketToServer = new Socket("localhost", 15000); ObjectOutputStream outStream = new ObjectOutputStream(socketToServer.getOutputStream()); for (int i=1; I Println("Sending object to server ..."); outStream. WriteObject("test message #"+i); } System.
Exit(0); } static class ServerListener extends Thread { private ServerSocket serverSocket; ServerListener() throws IOException { serverSocket = ServerSocketFactory.getDefault(). CreateServerSocket(15000); } @Override public void run() { while (true) { try { final Socket socketToClient = serverSocket.accept(); ClientHandler clientHandler = new ClientHandler(socketToClient); clientHandler.start(); } catch (IOException e) { e.printStackTrace(); } } } } static class ClientHandler extends Thread{ private Socket socket; ObjectInputStream inputStream; ClientHandler(Socket socket) throws IOException { this. Socket = socket; inputStream = new ObjectInputStream(socket.getInputStream()); } @Override public void run() { while (true) { try { Object o = inputStream.readObject(); System.out.
Println("Read object: "+o); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } } } In this example Strings are sent trough the ObjectStream. If you get the ClassNotFoundException (http://download.oracle.com/javase/6/docs/api/java/io/ObjectInputStream.html#readObject()) and are using an independent client and server program than you might check if both the client and the server have the class of the object to send in their class paths.
Ohhh I seee ... thanks for that buddy.. I will try it later .. and tell you how it goes.... – Mohammad Mustafa Hossaini Aug 11 '11 at 7:59 I tried it and it didn't work .... – Mohammad Mustafa Hossaini Aug 12 '11 at 4:43.
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.