本文整理匯總了Java中java.nio.channels.DatagramChannel.receive方法的典型用法代碼示例。如果您正苦於以下問題:Java DatagramChannel.receive方法的具體用法?Java DatagramChannel.receive怎麽用?Java DatagramChannel.receive使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.channels.DatagramChannel
的用法示例。
在下文中一共展示了DatagramChannel.receive方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doReadMessages
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
DatagramChannel ch = javaChannel();
UkcpServerChannelConfig config = config();
RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
ByteBuf data = allocHandle.allocate(config.getAllocator());
allocHandle.attemptedBytesRead(data.writableBytes());
boolean free = true;
try {
ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
int pos = nioData.position();
InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
if (remoteAddress == null) {
return 0;
}
allocHandle.lastBytesRead(nioData.position() - pos);
buf.add(UkcpPacket.newInstance(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()),
remoteAddress));
free = false;
return 1;
} catch (Throwable cause) {
PlatformDependent.throwException(cause);
return -1;
} finally {
if (free) {
data.release();
}
}
}
示例2: doIt
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
private static void doIt(DatagramChannel dc) throws IOException {
ByteBuffer bb = ByteBuffer.allocate(1024);
SocketAddress sa = dc.receive(bb);
bb.flip();
dc.send(bb, sa);
dc.close();
}
示例3: receive
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
@Override
protected SocketAddress receive(DatagramChannel handle, IoBuffer buffer) throws Exception {
return handle.receive(buffer.buf());
}