I tried to handle UDP connections exactly as TCP connections in my thread, and I got exceptions:
java.net.PortUnreachableException
at sun.nio.ch.DatagramDispatcher.read0(Native Method)
at sun.nio.ch.DatagramDispatcher.read(DatagramDispatcher.java:25)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:233)
at sun.nio.ch.IOUtil.read(IOUtil.java:206)
at sun.nio.ch.DatagramChannelImpl.read(DatagramChannelImpl.java:312)
at OverConn.OverChannel.read(OverChannel.java:75)
at OverConn.OverConn.run(OverConn.java:42)
This is the code that creates the DatagramChannel:
case UDP: {
channel = DatagramChannel.open();
((DatagramChannel)channel).connect(new InetSocketAddress(host, port));
((DatagramChannel)channel).configureBlocking(false);
break;
}
And then I register it at a appropriate time using:
case UDP: key = ((DatagramChannel)channel).register(selector, SelectionKey. OP_READ| SelectionKey. OP_WRITE); break;
Then every cycle I read it and decrypt exactly as any TCP stream using read(buffer);
Any idea?