Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT: I've added an array in the receiver to close all streams when the receiver stops.</p> <p>You should redesign your protocol layer. In both devices you have to create a <code>ServerSocket</code> to listen for new <code>Socket</code>s. Obviously, if you call any <code>read()</code> method the current thread will enter in a blocked state, so you need to use a secondary thread. You need to start() and stop() the receiver and use a listener to notify the socket creations. A possible implementation (it can be improved a lot, but the core is this):</p> <p><strong>Receiver.java</strong></p> <pre><code>public class Receiver{ private ArrayList&lt;SocketStream&gt; socketStreams; private OnNewSocketListener listener; private ServerSocket server; private Thread thread; private int port; public static interface OnNewSocketListener{ void onNewSocket (Stream stream); } public Receiver (int port, OnNewSocketListener listener){ this.listener = listener; this.port = port; } public synchronized start (){ if (thread != null) return; server = new ServerSocket (port); thread = new Thread (new Runnable (){ @Override public void run (){ try{ running = true; while (running){ socketStreams.add (stream); //See Stream.java below listener.onNewSocket (new Stream (server.accept ())); } }catch (SocketException e){ //stop() has been called }catch (IOException e){ //Error handling } } }).start (); } } public synchronized void stop (){ if (thread == null) return; running = false; try{ if (server != null){ server.close (); } }catch (IOException e){} for (SocketStream stream: socketStreams){ stream.close (); } socketStreams.clear (); thread = null; } } </code></pre> <p>Then you need another class that starts a thread when you want to read and write to this socket. To read you need another thread. You also need another listener to notify the object read and to notify when the other device closes the stream. You need a startReading() and close() methods (if you stop reading the socket it will close):</p> <p><strong>Stream.java</strong></p> <pre><code>public class Stream{ private Socket socket; private OnCloseStreamListener closeListener; private OnReadObjectListener readListener; private ObjectInputStream in; private ObjectOutputStream out; private Thread thread; public static interface OnReadObjectListener{ void onReadObject (Object obj); } public static interface OnCloseStreamListener{ void onCloseStream (); } //Used by the receiver to create an input socket public Stream (Socket socket){ this.socket = socket; out = new ObjectOutputStream (socket.getOutputStream ()); } //Used by the user to create an output socket, when the client wants to create a socket with the server public Stream (String address, int port){ socket = new Socket (address, port); out = new ObjectOutputStream (socket.getOutputStream ()); } public void setOnCloseStreamListener (OnCloseStreamListener listener){ closeListener = listener; } public void setOnReadObjectListener (OnReadObjectListener listener){ readListener = listener; } public synchronized void startReading (){ if (thread != null) return; thread = new Thread (new Runnable (){ @Override public void run (){ try{ in = new ObjectInputStream (socket.getInputStream ()); reading = true; while (reading){ Object obj = in.readObject (); if (obj == null){ //The other device has closed its socket stream reading = false; closeListener.onCloseSocketStream (); }else{ readListener.onReadObject (obj); } } }catch (SocketException e){ //stopReading() has been called }catch (IOException e){ //Error handling } } }).start (); } public synchronized void writeObject (Object obj){ out.writeObject (obj); out.flush; } public synchronized void close (){ if (thread != null){ reading = false; socket.close (); in.close (); out.close (); thread = null; }else{ socket.close (); in.close (); } } } </code></pre> <p>Usage:<br/> <strong>Server</strong></p> <pre><code>Receiver receiver = new Receiver (5000, new Receiver.OnNewSocketListener (){ @Override void onNewSocket (Stream stream){ stream.setOnCloseStreamListener (new Stream.OnCloseStreamListener (){ @Override void onCloseStream (){ //Stream is closed automatically, don't need to call close() //Do something } }); stream.setOnReadObjectListener (new Stream.OnReadObjectListener (){ @Override void onReadObject (Object obj){ //Do something with obj if (obj.isDoingSomeMaliciousActivities ()){ stream.close (); }else if (obj.isDoingGoodStuff){ stream.writeObject (new GoodStuff ()); } } }); stream.startReading (); } }); receiver.start (); Thread.sleep (10000); receiver.stop (); </code></pre> <p><strong>Client</strong></p> <pre><code>Stream stream = new Stream ("localhost", 5000); stream.setOnCloseStreamListener (new Stream.OnCloseStreamListener (){ @Override void onCloseStream (){ //Stream is closed automatically, don't need to call close() //Do something } }); stream.setOnReadObjectListener (new Stream.OnReadObjectListener (){ @Override void onReadObject (Object obj){ //Do something with obj if (obj.isDoingSomeMaliciousActivities ()){ stream.close (); }else if (obj.isDoingGoodStuff){ stream.writeObject (new GoodStuff ()); } } }); stream.startReading (); if (iHaveAGoodDay){ stream.writeObject (new IamHappy ()); }else{ stream.writeObject (new IwillHackYou ()); } Thread.sleep (10000); stream.close (); </code></pre> <p>This code is the core of the socket layer. It won't work because I don't tested it. You first need to understand the code to proceed with the protocol.<br/> Note: Don't be afraid to use all the listeners you think you need because you are constructing a layer that <strong>notifies</strong> events. It's like the user interaction when presses a button but with sockets.</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.
    3. 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