MulticastSocket 失败后未响应

发布于 2024-07-13 23:20:14 字数 703 浏览 5 评论 0原文

尝试在 MulticastSocket 上调用 joinGroup(addr) 时出现 SocketException。 这种情况仅发生在我们设置为在机器启动时自动启动应用程序的 Windows 机器上。

似乎抛出异常是因为 Windows 尚未完全完成其启动过程,这是异常。

java.net.SocketException: error setting options

    at java.net.PlainDatagramSocketImpl.join(Native Method)

    at java.net.PlainDatagramSocketImpl.join(Unknown Source)

    at java.net.MulticastSocket.joinGroup(Unknown Source)

在我们的应用程序启动时,如果我们在尝试加入该组之前等待一分钟,一切都会正常。

因此,我们决定放入一个重试循环,以便在网络可用时立即连接,这似乎可以工作。 两次失败后,第三次加入该组的尝试成功了。

问题是,现在 MulticastSocket 没有收到来自该组的任何消息,即使它加入得很好。

我在每次失败后创建一个新的 MulticastSocket 并丢弃旧的。

为什么在一个 MulticastSocket 上加入组失败会影响没有任何错误地加入的组,我该如何解决这个问题?

I get a SocketException when trying to call joinGroup(addr) on MulticastSocket. This is only happening on a Windows machine that we have setup to auto start our appliction when the machine is booted up.

It seems like the exception is thrown because Windows has not completely finished its startup process and here is the exception.

java.net.SocketException: error setting options

    at java.net.PlainDatagramSocketImpl.join(Native Method)

    at java.net.PlainDatagramSocketImpl.join(Unknown Source)

    at java.net.MulticastSocket.joinGroup(Unknown Source)

On startup of our app, if we wait a minute before trying to join the group, everything works fine.

So we decided to put in a retry loop so that it will connect as soon as the network is available which seemed to work. After two failures, the third attempt to join the group works.

The problem is, now the MulticastSocket does not receive any messages from the group, even though it joined up fine.

I am creating a new MulticastSocket after each failure and discarding the old one.

Why would a failure to join the group on one MulticastSocket affect the one that joined without any errors, and how could I possibly work around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隱形的亼 2024-07-20 23:20:14

我从来没有找出为什么套接字在成功加入组后不会收到消息。 不过,我确实想出了一个解决方法。

我循环检查所有网络接口,并确保列表中有一个有效的网络接口并且它已启动并正在运行。 我要做的下一件事是尝试在 MulticastSocket 上设置该网络接口。 如果这些测试通过,那么我让套接字尝试加入该组。 这似乎有效,但我仍然想了解更多幕后发生的事情。

private void validateNetworkInterfaces() throws IOException {

    Enumeration nis = NetworkInterface.getNetworkInterfaces();
    List<NetworkInterface> nics = new ArrayList<NetworkInterface>();
    while (nis.hasMoreElements()) {
        NetworkInterface ni = (NetworkInterface) nis.nextElement();

        logger.debug("nic name: " + ni.getDisplayName());
        logger.debug("nic isLoopback(): " + ni.isLoopback());
        logger.debug("nic isPointToPoint(): " + ni.isPointToPoint());
        logger.debug("nic isVirtual(): " + ni.isVirtual());
        logger.debug("nic isUp(): " + ni.isUp());
        logger.debug("nic supportsMulticast(): " + ni.supportsMulticast());

        if (!ni.isLoopback() && !ni.isPointToPoint() && !ni.isVirtual() && ni.isUp() && ni.supportsMulticast()) {
            logger.debug("adding nic: " + ni.getDisplayName());
            nics.add(ni);               
        }               

    }

    //check to make sure at least one network interface was found that supports multicast.
    if (nics.size() == 0) throw new SocketException("No network interfaces were found that support multicast.");

    //make sure the network interface can be set on a multicast socket
    for (NetworkInterface nic : nics) {
        logger.debug("attempting to set network interface on nic: " + nic.getDisplayName());
        MulticastSocket ms1 = new MulticastSocket(45599);
        ms1.setNetworkInterface(nic);
    }

}

I never did find out WHY the socket would not recieve messages after joining the group successfully. I did come up with a work around, however.

I loop through all the network interfaces and make sure there is a valid one in the list and it is up and running. The next thing I do is try setting that network interface on a MulticastSocket. If these tests pass, then I let the socket try to join the group. It seems to work, but I still would like to know more about what is going on behind the scenes.

private void validateNetworkInterfaces() throws IOException {

    Enumeration nis = NetworkInterface.getNetworkInterfaces();
    List<NetworkInterface> nics = new ArrayList<NetworkInterface>();
    while (nis.hasMoreElements()) {
        NetworkInterface ni = (NetworkInterface) nis.nextElement();

        logger.debug("nic name: " + ni.getDisplayName());
        logger.debug("nic isLoopback(): " + ni.isLoopback());
        logger.debug("nic isPointToPoint(): " + ni.isPointToPoint());
        logger.debug("nic isVirtual(): " + ni.isVirtual());
        logger.debug("nic isUp(): " + ni.isUp());
        logger.debug("nic supportsMulticast(): " + ni.supportsMulticast());

        if (!ni.isLoopback() && !ni.isPointToPoint() && !ni.isVirtual() && ni.isUp() && ni.supportsMulticast()) {
            logger.debug("adding nic: " + ni.getDisplayName());
            nics.add(ni);               
        }               

    }

    //check to make sure at least one network interface was found that supports multicast.
    if (nics.size() == 0) throw new SocketException("No network interfaces were found that support multicast.");

    //make sure the network interface can be set on a multicast socket
    for (NetworkInterface nic : nics) {
        logger.debug("attempting to set network interface on nic: " + nic.getDisplayName());
        MulticastSocket ms1 = new MulticastSocket(45599);
        ms1.setNetworkInterface(nic);
    }

}
云淡风轻 2024-07-20 23:20:14

我知道这已经很旧了,但是可靠的多播答案似乎很少见。

我认为你会更好:

final InetAddress localHost = InetAddress.getLocalHost();
final NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);

因为这更简洁,并且还可以确保获得一个实际上会接收多播消息的NIC。

I know this is old, but solid multicast answers seem rare.

I think you would be better off with:

final InetAddress localHost = InetAddress.getLocalHost();
final NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);

As this is more succinct and would also ensure that are getting a NIC that will actually be receiving the multicast message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文