Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>EOFException in your code</h1> <p>The problem seems to be your <code>while (to.value != 1)</code>. The server receives the first object, with value 1. It does not execute that while loop at all, but instead closes the socket immediately. So when the client tries to create an <code>ObjectInputStream</code> from the input stream of that socket, it encounters a closed connection where it would expect an object stream header. Thus the <code>EOFException</code>.</p> <h1>Implementing a conversation</h1> <p>The easiest way to implement a conversation is avoiding the loops altogether. After all, you do the loop but then do a case distinction within the loop to execute special code in each pass. You might as well write a linear program, and factor common code into method calls if needed, although your example has little common code.</p> <pre><code>class SimpleClient { public static void main(String args[]) throws Exception { // Connection setup Socket s = new Socket("localhost",2002); OutputStream os = s.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); InputStream is = s.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); testobject to; // Conversation System.out.println("sending object 1"); to = new testobject(1,"object 1","field1","field2","field3","field4"); oos.writeObject(to); System.out.println("receiving object 2"); to = (testobject)ois.readObject(); System.out.println(to.id); System.out.println("sending object 3"); to = new testobject(2,"object 3","field1","field2","field3","field4"); oos.writeObject(to); System.out.println("receiving object 4"); to = (testobject)ois.readObject(); System.out.println(to.id); // Connection shutdown ois.close(); oos.close(); s.close(); } } class SimpleServer { public static void main(String args[]) throws Exception { // Connection setup ServerSocket ss = new ServerSocket(2002); Socket s = ss.accept(); // only handle a single connection ss.close(); // so we can immediately stop listening for more OutputStream os = s.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); InputStream is = s.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); testobject to; // Conversation System.out.println("receiving object 1"); to = (testobject)ois.readObject(); System.out.println(to.id); System.out.println("sending object 2"); to = new testobject(2,"object 2","field1","field2","field3","field4"); oos.writeObject(to); System.out.println("receiving object 3"); to = (testobject)ois.readObject(); System.out.println(to.id); System.out.println("sending object 4"); to = new testobject(4,"object 4","field1","field2","field3","field4"); oos.writeObject(to); // Connection shutdown ois.close(); oos.close(); s.close(); } } </code></pre> <h1>Send and receive queues</h1> <p>If you really want to send stuff from a queue, you can simply iterate over it.</p> <pre><code>import java.net.*; import java.io.*; import java.util.*; class SimpleClient { public static void main(String args[]) throws Exception { // Preparing the queues List&lt;testobject&gt; sendQueue = Arrays.asList( new testobject(1,"object 1","field1","field2","field3","field4"), new testobject(2,"object 3","field1","field2","field3","field4")); List&lt;testobject&gt; receiveQueue = new ArrayList&lt;testobject&gt;(); // Connection setup Socket s = new Socket("localhost",2002); OutputStream os = s.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); InputStream is = s.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); testobject to; // Conversation, we start by sending for (testobject toSend: sendQueue) { System.out.println("Sending " + toSend.id); oos.writeObject(toSend); testobject received = (testobject)ois.readObject(); System.out.println("Received " + received.id); receiveQueue.add(received); } // Connection shutdown ois.close(); oos.close(); s.close(); } } class SimpleServer { public static void main(String args[]) throws Exception { // Preparing the queues List&lt;testobject&gt; sendQueue = Arrays.asList( new testobject(2,"object 2","field1","field2","field3","field4"), new testobject(4,"object 4","field1","field2","field3","field4")); List&lt;testobject&gt; receiveQueue = new ArrayList&lt;testobject&gt;(); // Connection setup ServerSocket ss = new ServerSocket(2002); Socket s = ss.accept(); // only handle a single connection ss.close(); // so we can immediately stop listening for more OutputStream os = s.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); InputStream is = s.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); testobject to; // Conversation, we start by receiving for (testobject toSend: sendQueue) { testobject received = (testobject)ois.readObject(); System.out.println("Received " + received.id); receiveQueue.add(received); System.out.println("Sending " + toSend.id); oos.writeObject(toSend); } // Connection shutdown ois.close(); oos.close(); s.close(); } } </code></pre> <p>But since in this case, all the data is known up front, you might save some network round trips by not interleaving things, but instead sending all data at both ends immediately, using a separate thread to receive things. However, that would be even further away from your example, so I'll not include code for this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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