使用Java DatagramChannel监听UDP数据报
问题描述:
我想绑定一个Java DatagramChannel来侦听特定端口和子接口上的UDP流量,但我似乎无法让它在我的linux主机上工作,并在其中创建了多个虚拟网络子接口(特别是我想听的一个)如下所示。如果我将Java应用程序托管在同一网络Windows PC中(使用不同的IP地址,但是在这种情况下,我只需指定侦听端口并在Windows上不使用子接口),代码就好了。使用Java DatagramChannel监听UDP数据报
这是使用ifconfig的相关部分输出上表示虚拟网络适配器(子接口)
p2p1:37: flags=4163<UP,BnetROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.34.1 netmask 255.255.0.0 broadcast 192.168.255.255
ether 00:13:72:a5:9c:e2 txqueuelen 1000 (Ethernet)
device interrupt 16
接口是设置如下我的Linux框:
ifconfig p2p1:37 192.168.34.1 netmask 255.255.0.0
我使用的代码初始化非阻塞DatagramChannel如下所示:(我需要一个DatagramChannel
,因为我需要使用选择器以非阻塞方式同时从多个端口读取)。我尝试使用通过channel.socket().bind(new InetSocketAddress(entry.getValue())
存储在entry.getValue()
中的端口号绑定到默认适配器,但没有收到任何数据包。我在某个地方读到DatagramChannel构建完成后,默认情况下会构造一个绑定到'0.0.0.0'的数据报套接字,绑定一旦绑定就不能解除绑定,但我不确定。任何指导将不胜感激。
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
updateMessage("Running...");
Map<String, DatagramChannel> dlmuChannelInfo = new HashMap<>();
Map<String, DatagramChannel> wsuChannelInfo = new HashMap<>();
try {
// array of bytes for receiving datagrams
ByteBuffer rxBuffer = ByteBuffer.allocateDirect(MAX_PACKET_SIZE);
for (Map.Entry<String, Integer> entry : mDLMUPortInfo.entrySet()) {
DatagramChannel channel = DatagramChannel.open();
// select only works with nonblocking channels
channel.configureBlocking(false);
// NetworkInterface iface = NetworkInterface.getByInetAddress(
// InetAddress.getByName("192.168.34.1"));
// List<NetworkInterface> list = Collections.list(
// NetworkInterface.getNetworkInterfaces());
// for (NetworkInterface next : list) {
// }
channel.socket().bind(new InetSocketAddress(
InetAddress.getByName("192.168.34.1"), entry.getValue()));
dlmuChannelInfo.put(entry.getKey(), channel);
}
// register for reads
try (
// instantiate a selector - note that this autocloses
Selector selector = Selector.open()) {
// register a selector for reads for both the DLMU & WSU
Map<SelectionKey, String> keyServiceInfo = new HashMap<>();
for (Map.Entry<String, DatagramChannel> entry : dlmuChannelInfo.entrySet()) {
SelectionKey key = entry.getValue().register(
selector, SelectionKey.OP_READ);
keyServiceInfo.put(key, entry.getKey());
}
答
您正在绑定到192.168.34.1。您只会收到发送到该接口的数据报。除非你真的知道你在做什么,你应该绑定到0.0.0.0,这是通过完成的,省略了,InetAddress
参数,或者使用null。
您对DatagramSocket
做的评语本身就是正确的。
@EJB我尝试过,但不幸的是它不工作,我有一个FTP服务器也运行在这个虚拟接口,我可以很好地登录,所以IP地址是活着的。我遇到的唯一问题是由于它是一个子接口。我甚至尝试通过子接口枚举,并从特定的子接口获得InetAddress,并使用它无济于事。 – johnco3