这是我的代码:
channel = DatagramChannel.open();
socket = channel.socket();
channel.configureBlocking(false);
socket.bind(new InetSocketAddress(3000));
selector = Selector.open();
channel.register(selector, SelectionKey.OP_READ);
ByteBuffer buffer = ByteBuffer.allocate(65536);
while(true)
{
if(selector.select()>0)
{
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator iterator = selectionKeys.iterator();
while(iterator.hasNext())
{
SelectionKey key = (SelectionKey)iterator.next();
iterator.remove();
InetSocketAddress isa = (InetSocketAddress) channel.getRemoteAddress();
if(key.isReadable())
{
System.out.print(isa.getAddress().getHostAddress()+":"+isa.getPort());
}
}
}
}
isa 为空。我想获取 DatagramPack SocketAddress,如 socket.receive(DatagramPack); 但我不知道如何获取它。使用 Channel.getSocketAddress() 返回 Null。