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.