Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Check out the Java <code>Serializable</code> interface: <a href="http://docs.oracle.com/javase/6/docs/api/java/io/Serializable.html" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/java/io/Serializable.html</a></p> <p>In summary, a class that implements <code>Serializable</code> can be serialized with no additional work if all of its fields are also <code>Serializable</code>. So:</p> <pre><code>public class Message&lt;Img_Type&gt; implements Serializable { protected String mText = null; protected &lt;Img_Type&gt; mImage = null; // Img_Type must implement Serializable // ... constructor(s), get/set methods } </code></pre> <p>You can then write and read these objects using <code>Socket</code>s, <code>ObjectInputStream</code>, and <code>ObjectOutputStream</code>. To read (one) <code>Message</code>:</p> <pre><code> Socket socket; // initialize your socket Message msg; try { InputStream is = socket.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); Message msg = (Message) ois.readObject(); ois.close(); } catch (ClassNotFoundException e) { // handle exception } // handle message </code></pre> <p>Writing a <code>Message</code> can be done in a similar manner using <code>ObjectOutputStream</code>. In this example, you can check the <code>Message</code> fields against <code>null</code> to see what it contains.</p> <p>One thing to be conscious of is that the constructor for <code>ObjectInputStream</code> will not return until it reads the header of an <code>Object</code> from the input stream, e.g. a <code>Message</code> is ready to be received. Additionally, closing the object input stream on the server will throw an exception on the client if they have a corresponding output stream open, and vice versa.</p>
 

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