Note that there are some explanatory texts on larger screens.

plurals
  1. PORe-use a java socket client once again
    text
    copied!<p>My question is very simple. I just want to use a client object socket once again.</p> <p>Typically people create a new socket any time the client tries to connect the server such:</p> <pre><code> Socket test = new Socket (host, port); OutputStream out = test.getOutputStream (); InputStream in = test.getInputStream (); out.writeObject (object) out.flush (); in.readObject(); </code></pre> <p>After the first writeObject I do want again to send more information to the server by reusing out.writeObject(nextObject) however the server is no longer reached. In order to get it working I have to go through all the steps above. Why?</p> <hr> <p>Here is the code I am using to test what I meant. The code after "Second Request doesn't ..." does not reach the server. However if I remove the comments just above those comments it will go through. Why cannot I re-use the same client socket object without having to use new Socket ().</p> <pre><code>Class Hello -&gt; Data passed between the server and the client Class TestClient -&gt; Client Class TestServer -&gt; Server package tests; import java.io.Serializable; public class Hello implements Serializable { String hola = "hello my friend"; public String getHello () { return hola; } public void setHello (String str) { hola = str; } } package tests; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.net.UnknownHostException; public class TestClient { public static void main(String[] args) { try { ObjectOutputStream clientOutput; ObjectInputStream serverInput; Hello goingout = new Hello (); Socket client = new Socket ("localhost", 5555); clientOutput = new ObjectOutputStream (client.getOutputStream()); serverInput = new ObjectInputStream (client.getInputStream()); clientOutput.writeObject(goingout); clientOutput.flush(); Hello comingin = (Hello) serverInput.readObject(); System.out.println ("first time received: " + comingin.getHello()); /* * Socket client = new Socket ("localhost", 5555); clientOutput = new ObjectOutputStream (client.getOutputStream()); serverInput = new ObjectInputStream (client.getInputStream()); */ /* * Second request doesn't got through the server */ comingin.setHello("again"); clientOutput.writeObject(goingout); clientOutput.flush(); comingin = (Hello) serverInput.readObject(); System.out.println ("second time received: " + comingin.getHello()); } catch (ClassNotFoundException ex) { System.out.println (ex.getMessage()); } catch (UnknownHostException ex) { System.out.println (ex.getMessage()); } catch (IOException ex) { System.out.println (ex.getMessage()); } } } public class TestServer { public static void main(String[] args) { try { ServerSocket server = new ServerSocket(5555, 100); ObjectInputStream clientInput; ObjectOutputStream serverOutput; for (;;) { System.out.println ("Listening for connections"); Socket fromClient = server.accept(); serverOutput = new ObjectOutputStream (fromClient.getOutputStream()); clientInput = new ObjectInputStream (fromClient.getInputStream()); Hello received = (Hello) clientInput.readObject(); serverOutput.writeObject(received); serverOutput.flush(); } } catch (ClassNotFoundException ex) { Logger.getLogger(TestServer.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(TestServer.class.getName()).log(Level.SEVERE, null, ex); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload