Ignoring your own UDP broadcasts in Java(在 Java 中忽略您自己的 UDP 广播)
问题描述
在我的程序中,我发送 UDP 广播并对它们做出反应.我需要一种方法来忽略 我 发出的 UDP 广播,但对不是来自我的机器的广播做出反应.
In my program I'm sending out UDP broadcasts and reacting to them. I need a way to ignore the UDP broadcasts that I send out, but react to ones that aren't from my machine.
我确实尝试过使用:if (NetworkInterface.getByInetAddress(packet.getAddress()) != null)但这在某些情况下会产生 IOExceptions(java.net.SocketException: no network interface is bound to such an IP address)
I did try using:
if (NetworkInterface.getByInetAddress(packet.getAddress()) != null)
but this generated IOExceptions in some cases (java.net.SocketException: no network interface is bound to such an IP address)
有人有什么想法吗?
还有:我的套接字上的 getInetAddress() 抛出 NullPointerException
Also: getInetAddress() on my socket throws a NullPointerException
推荐答案
我觉得javadoc和NetworkInterface getByInetAddress()的实际实现有点出入.javadoc 似乎建议 getByInetAddress 如果未找到匹配项将返回 null,但实现要么返回匹配项,要么抛出 SocketException.
I think there's a little discrepancy between the javadoc and the actual implementation of NetworkInterface getByInetAddress(). The javadoc seems to suggest that getByInetAddress would return null if no match was found, yet the implementation either returns a match, either throws a SocketException.
public static NetworkInterface getByInetAddress(InetAddress addr)
                                               throws SocketException
返回:一个 NetworkInterface 或 如果没有具有指定 IP 地址的网络接口,则返回 null.
Returns: A NetworkInterface or null if there is no network interface with the specified IP address.
  public static NetworkInterface getByInetAddress (InetAddress addr)
    throws SocketException
  {
    if (networkInterfaces == null)
      networkInterfaces = getRealNetworkInterfaces ();
    for (Enumeration interfaces = networkInterfaces.elements ();
         interfaces.hasMoreElements (); )
      {
        NetworkInterface tmp = (NetworkInterface) interfaces.nextElement ();
        for (Enumeration addresses = tmp.inetAddresses.elements ();
             addresses.hasMoreElements (); )
          {
            if (addr.equals ((InetAddress) addresses.nextElement ()))
              return tmp;
          }
      }
    throw new SocketException (
      "no network interface is bound to such an IP address");
  }
我建议要么捕获异常并将其视为来自第 3 方的答案,要么使用 getNetworkInterfaces() 方法重新实现它.
I suggest to either catch the exception and treat it as an answer from a 3rd party, either re-implement it using the getNetworkInterfaces() method.
这篇关于在 Java 中忽略您自己的 UDP 广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中忽略您自己的 UDP 广播
				
        
 
            
        - 如何指定 CORS 的响应标头? 2022-01-01
 - 转换 ldap 日期 2022-01-01
 - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
 - 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
 - 获取数字的最后一位 2022-01-01
 - java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
 - 未找到/usr/local/lib 中的库 2022-01-01
 - 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
 - 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
 - Eclipse 的最佳 XML 编辑器 2022-01-01
 
						
						
						
						
						
				
				
				
				