NIO Object Streams

Hey all!

I’m writing a 3D tennis game at the moment for my final-year college project, and I’m trying to get it to work over a network. To be honest, I was unaware of NIO until quite recently, but it seemed very straightforward and was exactly what I was looking for, so I went ahead and started writing the network code using it. Unfortunately, I’ve hit a problem.

Basically, I have a Message class which encapsulates all the data I need to send for each event - message ID, player/ball positions and directions, etc. I want to create an Object stream across the socket channel, so I can serialise instances of this class and transmit them back and forth. Whenever I try to set up the stream, however, I get an IllegalBlockingModeException. I’ve checked through the code, and found that the connection is set up correctly and no exceptions are thrown UNTIL I attempt to create an Object Stream on the channel. I had assumed that the ObjectStream would behave exactly as it would when created on a regular Socket, but would return immediately with null if there was no incoming data. From this exception, however, it seems that Object streams are blocking-specific.

My question is - how can I set up object input/output streams on a non-blocking channel? Are there some permission issues to take into account that I’m not aware of, or are there updated Object stream classes which are designed specifically for NIO? Any help would be much appreciated!

Thanks,
Reynard.


SocketChannel sChannel;

public void setupConnection(boolean client)
{      ServerSocketChannel servChannel;

      try
      {      if(!client)
            {      servChannel = ServerSocketChannel.open();
                  servChannel.configureBlocking(true);
                  servChannel.socket().bind(new InetSocketAddress(4096));

                  sChannel = servChannel.accept();
                  sChannel.configureBlocking(false);

                  servChannel.close();
            }
            else
            {      sChannel = SocketChannel.open();
                  sChannel.configureBlocking(false);
                  sChannel.connect(new InetSocketAddress(hostName, 4096));

                  while(!sChannel.finishConnect())
                        Thread.sleep(25);
            }

            in = new ObjectInputStream(Channels.newInputStream(sChannel));
            out = new ObjectOutputStream(Channels.newOutputStream(sChannel));                        
      }
      catch(Exception e)
      {      System.err.println("Error while establishing connection. Terminating... \n" + e.getMessage());
            System.exit(-1);
      }
}

I should also mention that the ServerSocketChannel in the above example is discarded because this is a straightforward 2-player game - once the first connection is accepted, there is no need to listen for any more. As such, I configured the ServerSocketChannel to block intentionally - I assumed that this wouldn’t cause any problems, since I could just set the resulting SocketChannel to non-blocking.

I’m no expert at this (yet ;)), but I think your problem is that you’re trying to use java.io streams which inherently block. If you want to do non-blocking I/O, you should use the SocketChannel read/write methods. In fact, I’d go as far as to say you should write your own serialize/deserialize methods on the Message object that return/accept ByteBuffers.

Yeah, I figured as much :-/ But here’s a whole NEW thing; I just tried using the traditional Socket and ServerSocket, and the connection itself again worked fine. BUT, when I tried instantiating object input/output streams using the socket’s streams, the program stopped dead! I tried wrapping a BufferedReader and PrintWriter around the streams, just to see what’d happen - and it worked perfectly. ??? It’s beginning to look like I’ll have to communicate using Strings and such, which is exactly what I wanted to avoid in the first place. :frowning:

Does anyone see what might be causing the problem here?


Socket sChannel;
ServerSocket servChannel;

ObjectInputStream in;
ObjectOutputStream out;

public void setupConnection(boolean client)
{
      try
      {      if(!client)
            {      servChannel = new ServerSocket(4096);
                  sChannel = servChannel.accept();
            }
            else
                  sChannel = new Socket(hostName, 4096);

            in = new ObjectInputStream(sChannel.getInputStream());
            out = new ObjectOutputStream(sChannel.getOutputStream());                        
      }
      catch(Exception e)
      {      System.err.println("Error while establishing connection. Terminating... \n" + e.getMessage());
            System.exit(-1);
      }
}

Aha - it turns out that creating the ObjectInputStream before the ObjectOutputStream can cause programs to hang. I just changed the order, and it worked.