// array of bytes for receiving datagrams
byte[] receiveData = new byte[ datagramSize ];
ByteBuffer theReceiveDataByteBuffer = ByteBuffer.wrap( receiveData );
NIOTest()
{
try
{
Random theRandom = new Random();
InetSocketAddress theInetSocketAddress = new InetSocketAddress( host, port);
// make a DatagramChannel
DatagramChannel theDatagramChannel = DatagramChannel.open();
theDatagramChannel.configureBlocking( false );
// instantiate a selector
Selector theSelector = Selector.open();
// register the selector on the channel to monitor reading
// datagrams on the DatagramChannel
theDatagramChannel.register( theSelector, SelectionKey.OP_READ );
long millisecsUntilSendNextDatagram = 0;
int i = 1; int j = 1;
// send and read concurrently, but do not block on read:
while (true)
{
long start = System.currentTimeMillis();
// which comes first, next send or a read?
// in case millisecsUntilSendNextDatagram <= 0 go right to send
if ( millisecsUntilSendNextDatagram <= 0 ||
theSelector.selectNow() == 0 )
{
// just for fun, send between 0 and 4 datagrams
for( int k = 0; k < theRandom.nextInt( 5 ); k++ )
{