T
Timo Nentwig
Hi!
I'm too stupid to utilize DatagramChannel. What I do is actually quite
simple: I do have a query and a list of servers. I send the query to the
first server and if it is able to help me I'm done, otherwise I send the
query to the next server in the list.
This is what it looks like with the old I/O API:
public Result go(byte[] query)
{
DatagramSocket sock = new DatagramSocket();
sock.setSoTimeout(1000);
for (int i=0; i<servers.length; i++)
{
try
{
DatagramPacket packet = new DatagramPacket (query, query.length,
servers, PORT);
sock.send(packet);
packet = new DatagramPacket(new byte[1024],1024);
sock.receive(packet);
return new Result(packet.getData(),0,packet.getLength());
}
catch (Exception e)
{
// try next server
continue;
}
}
}
What is this going to look like using NIO?? I think my problem is a
misunderstanding of ByteBuffer.clear(), .flip() and .limit() (capacity(),
remaining()).
Ok, here how I thought I ought to work:
DatagramChannel dc = DatagramChannel.open();
dc.socket().setSoTimeout(1000); // correct?
// are there no dynamic buffers? I don't know how much
// data I might receive from the server...
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
for (int i=0; i<servers.length; i++)
{
try
{
InetSocketAddress target = new
InetSocketAddress(servers, PORT);
bb.clear();
bb.put(query); // bb.limit() remains 1024?
dc.send(bb, target);
bb.flip();
dc.receive(bb);
byte[] res = new byte[bb.limit()];
bb.get(res);
packet = new DatagramPacket(res, res.length);
return new Result(packet.getData(),0,packet.getLength());
}
catch (Exception e)
{
// try next server
continue;
}
}
This code does reflect my current state, I tried a lot and finally even
got it to work but only if the request to the first server did succeed...
What did I misunderstand about NIO?
Thanks for any advice...
Timo
I'm too stupid to utilize DatagramChannel. What I do is actually quite
simple: I do have a query and a list of servers. I send the query to the
first server and if it is able to help me I'm done, otherwise I send the
query to the next server in the list.
This is what it looks like with the old I/O API:
public Result go(byte[] query)
{
DatagramSocket sock = new DatagramSocket();
sock.setSoTimeout(1000);
for (int i=0; i<servers.length; i++)
{
try
{
DatagramPacket packet = new DatagramPacket (query, query.length,
servers, PORT);
sock.send(packet);
packet = new DatagramPacket(new byte[1024],1024);
sock.receive(packet);
return new Result(packet.getData(),0,packet.getLength());
}
catch (Exception e)
{
// try next server
continue;
}
}
}
What is this going to look like using NIO?? I think my problem is a
misunderstanding of ByteBuffer.clear(), .flip() and .limit() (capacity(),
remaining()).
Ok, here how I thought I ought to work:
DatagramChannel dc = DatagramChannel.open();
dc.socket().setSoTimeout(1000); // correct?
// are there no dynamic buffers? I don't know how much
// data I might receive from the server...
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
for (int i=0; i<servers.length; i++)
{
try
{
InetSocketAddress target = new
InetSocketAddress(servers, PORT);
bb.clear();
bb.put(query); // bb.limit() remains 1024?
dc.send(bb, target);
bb.flip();
dc.receive(bb);
byte[] res = new byte[bb.limit()];
bb.get(res);
packet = new DatagramPacket(res, res.length);
return new Result(packet.getData(),0,packet.getLength());
}
catch (Exception e)
{
// try next server
continue;
}
}
This code does reflect my current state, I tried a lot and finally even
got it to work but only if the request to the first server did succeed...
What did I misunderstand about NIO?
Thanks for any advice...
Timo