0
這是我的代碼:異步UDP如何從java中的客戶端獲取ip和端口?
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是null.I想要得到DatagramPack的SocketAddress像socket.receive(DatagramPack);但我不知道渠道如何得到它。使用Channel.getSocketAddress()retun Null。