Hi all NIO smarties please tell me why this code does not work
I also posted to the Java networking forums (Duke Dollars and all:-)) but there is always so much more action over here.
Here’s the set up, a simple DatagramChannel test app. This can’t be a BUG?
From my forum post:
“I have two class, one is reading, one is writing, using DatagramChannels that are connected, i.e. I am not using receive/send, I am using read/write. I say I am using but it’s not working, so I should say I WANT to. It does connect and I get one good read but then selector.select() blocks OR if I disconnect and reconnet everytime (as commented out) it works, but what’s the point of connecting then I could just use receive…”
What am I doing wrong?
Here’s the compilable code. Give it a try…
/*
* Main.java
*
* Created on June 9, 2005, 8:17 PM
*/
package network;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.net.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int id = 100;
try
{
// Create the server socket channel
DatagramChannel server = DatagramChannel.open();
// nonblocking I/O
server.configureBlocking(false);
// host-port 8000
String host = "LocalHost";
server.socket().bind(new java.net.InetSocketAddress(host,8000));
// Create the selector
Selector selector = Selector.open();
// Recording server to selector
server.register(selector,SelectionKey.OP_READ);
System.out.println(server);
// Infinite server loop
boolean connected = false;
for(;;)
{
Thread.currentThread().yield();
// Waiting for events
System.out.println("reading1...");
selector.select();
System.out.println("reading1.1...");
// Get keys
Set keys = selector.selectedKeys();
Iterator i = keys.iterator();
if ( !connected )
{
server.connect(new InetSocketAddress(host,8000));
connected = true;
}
// For each keys...
while(i.hasNext())
{
System.out.println("reading2...");
SelectionKey key = (SelectionKey) i.next();
// Remove the current key
i.remove();
//for(;;)
{
try
{
Thread.currentThread().sleep(500);
}
catch(Exception e)
{};
int BUFFER_SIZE = 32;
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
try
{
int count = server.read(buffer);
System.out.println(count);
}
catch (Exception e)
{
// client is no longer active
e.printStackTrace();
continue;
}
//client.disconnect();
// Show bytes on the console
buffer.flip();
Charset charset=Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(buffer);
System.out.print(charBuffer.toString());
}
}
}
}
catch(Exception e)
{e.printStackTrace();}
}
}
/*
* Client.java
*
* Created on June 10, 2005, 11:28 PM
*/
package network;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.net.*;
import java.util.*;
public class Client
{
public static void main(String[] args)
{
String host = "LocalHost";
int id = 100;
try
{
// Create client SocketChannel
DatagramChannel client = DatagramChannel.open();
// Connection to host port 8000
Thread.currentThread().sleep(5000);
client.connect(new java.net.InetSocketAddress(host,8000));
// nonblocking I/O
client.configureBlocking(false);
// Create selector
Selector selector = Selector.open();
// Record to selector
SelectionKey clientKey = client.register(selector, SelectionKey.OP_WRITE);
while (selector.select(500)> 0)
{
// Get keys
Set keys = selector.selectedKeys();
Iterator i = keys.iterator();
System.out.println("writing1...");
// For each key...
while (i.hasNext())
{
SelectionKey key = (SelectionKey)i.next();
System.out.println("writing2...");
// Remove the current key
i.remove();
// Get the socket channel held by the key
DatagramChannel channel = (DatagramChannel)key.channel();
// Write continuously on the buffer
ByteBuffer buffer = null;
for (;;)
{
try
{
Thread.currentThread().sleep(1000);
}
catch(Exception e)
{};
System.out.println("writing...");
buffer =
ByteBuffer.wrap(
new String(" Client " + id + " ").getBytes());
channel.write(buffer);
buffer.clear();
}
}
}
}
catch(Exception e)
{e.printStackTrace();}
}
}