title: Shifting from blocking to non-blocking I/O with javanio
body: <p>i adapt this code <a href="https://stackoverflow.com/questions/1453028/how-to-send-and-receive-serialized-object-in-socket-channel:for">How to send and receive serialized object in socket channel</a> my real time simulation to send objects but i am running into exceptions one after another is it because this code blocking in nature how this code can be converted in to non blocking with javanio  </p>
  
  <pre><code>  /*
       * Writer
       */
      import java.io.IOException;
      import java.io.ObjectOutputStream;
      import java.net.InetSocketAddress;
      import java.nio.channels.ServerSocketChannel;
      import java.nio.channels.SocketChannel;
  
      public class CleanSender implements Runnable {
  
          private SimManager SM;
          private BallState ballState = new BallState(10, 5);
          private ServerSocketChannel ssChannel;
  
          private Thread tRunSer = new Thread(this, "ServerSelectThread");
  
      public static void main(String[] args) throws IOException {
  
          CleanSender server = new CleanSender();
          server.startServer();
  
      }
  
      private void startServer() throws IOException {
          ssChannel = ServerSocketChannel.open();
          ssChannel.configureBlocking(true);
          int port = 2345;
          ssChannel.socket().bind(new InetSocketAddress(port));
          // SM = new SimManager(this, BS);
          // SM.start(); // GameEngine thread starting here
          tRunSer.start();
      }
  
      public void run() {
          try {
              SocketChannel sChannel = ssChannel.accept();
  
              while (true) {
  
                  ObjectOutputStream oos = new ObjectOutputStream(sChannel
                          .socket().getOutputStream());
                  oos.writeObject(ballState);
                  System.out.println("Sending String is: '" + ballState.X + "'" + ballState.Y);
                  oos.close();
                  System.out.println("Sender Start");
                  System.out.println("Connection ended");
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Client: which is continously looking  for reply from server</p>
  
  <pre><code>import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.net.InetSocketAddress;
  import java.nio.channels.SocketChannel;
  
  public class CleanReceiver implements Runnable {
  
      private SocketChannel sChannel;
      private Thread receiverThread = new Thread(this, "receiverThread");
  
  
      private synchronized  void startServer() throws IOException {
           sChannel = SocketChannel.open();
           sChannel.configureBlocking(true);
           if (sChannel.connect(new InetSocketAddress("localhost", 2345))) {
               receiverThread.start();
           }
      }
  public void run() {
  
      while (true) {
          try {
              ObjectInputStream ois = new ObjectInputStream(sChannel.socket()
                      .getInputStream());
  
              BallState s = (BallState) ois.readObject();
              System.out.println("String is: '" + s.X + "'" + s.Y);
              ois.close();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (ClassNotFoundException e) {
              e.printStackTrace();
          }
  
          System.out.println("End Receiver");
      }
  }
  
  
        public static void main(String[] args) 
          throws IOException, ClassNotFoundException {
  
              CleanReceiver rc=new CleanReceiver();
              rc.startServer();
  
              System.out.println("End Receiver");
          }
  }
  </code></pre>
  
  <p><strong><em>Will this design work in the scenario when server has to keep connect the client and simultaneous send the simulation state to already connected client?</em></strong>, i m looking for experts glance.</p>
  
  <p>thanks,</p>
  
  <p>jibbylala</p>