DatagramChannel.receive()

L

lightning

I use a selector to manage the unblocking DatagramChannel,
When I got the event that I can read from the channel
(key.isReadable()==true),
I call this method.

What I wanna know is - Is this method perform an I/O operation
at lower-layer or just copy bits from a place in the memory.

If it is the former, do I need to use multithread to invoke receive()
to make better performance???

this is part of my code:




channel.configureBlocking(false);
selector = Selector.open();
SelectionKey key = channel.register(selector, channel.validOps());

while (run) {

selector.select(250);

if (key.isValid() && key.isReadable()) {

ByteBuffer buffer = ByteBuffer
.allocate(Constant.MAX_RECEIVE_BUFFER_SIZE);
buffer.order(ByteOrder.LITTLE_ENDIAN);
InetSocketAddress sock = (InetSocketAddress) channel
.receive(buffer);
if (sock == null)
continue;
receivedDatagramCount++;
log.info("received No." + receivedDatagramCount
+ " datagram");
String ip = sock.getAddress().getHostAddress();
int port = sock.getPort();
if (!Crypt.decrypt(buffer.array(), buffer.position())) {
log.warn("checksum error$B!*(B");
return;
}

buffer.flip();
P2IHeaderInfo header = P2IHeaderInfo.getInstance(buffer);
buffer.rewind();

DispatchData data = new DispatchData(header, buffer, ip,
port, receivedDatagramCount);
Task task = new Task(Task.DISPATCH, data);
server.sendMessage(task);
}
if (key.isValid() && key.isWritable()) {

for(ToSendData data=resps.poll();data != null;data=resps.poll())
{
String ip = data.getIp();
int port = data.getPort();
ByteBuffer buffer = data.getBuffer();
Crypt.encrypt(buffer.array(), buffer.remaining());
byte[] x = new byte[buffer.remaining()];
System.arraycopy(buffer.array(), 0, x, 0, buffer
.remaining());
sendDatagramCount++;
log.info("Server sends No." + sendDatagramCount
+ " datagram: ");
StringUtil.printBytes(x);
channel.send(buffer, new InetSocketAddress(ip, port));
}
}
// }

}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (selector != null) {
try {
selector.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (channel != null) {
try {
channel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
 
E

EJP

lightning said:
What I wanna know is - Is this method perform an I/O operation
at lower-layer or just copy bits from a place in the memory.

It's the same thing. If OP_READ has fired, the data has already arrived
in the socket buffer, and calling receive() copies it into your ByteBuffer.
 
L

lightning

thx , just to ensure that when read fires,all those bits have already
arrived at the memory
 
E

EJP

lightning said:
thx , just to ensure that when read fires,all those bits have already
arrived at the memory
That follows from the definitions. OP_READ means a DatagramSocketChannel
is ready to read.A UDP datagram arrives intact or not at all. Ergo,
OP_READ means a complete UDP datagram is ready to read.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top