[newbie] DatagramChannel

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
 
T

Timo Nentwig

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? bb.flip();
dc.send(bb, target);

bb.flip(); bb.clear(); // no flip()
dc.receive(bb); bb.flip();
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;
}
}


Quite obvious actually...:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top