多播套接字-当计算机从睡眠中唤醒时将不起作用 [英] multicast socket - will not work when computer wakes up from sleep

查看:103
本文介绍了多播套接字-当计算机从睡眠中唤醒时将不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个多播套接字,该套接字正在接收数据包,并且工作正常.

Hi guys I have a multicast socket that is receiving packets and is working fine.

我遇到了一个问题,但是当我的计算机从睡眠状态唤醒时,多播套接字不起作用.持续超时:

I had an issue though where when my computer wakes up from sleep the multicast socket does not work. Continually timing out:

MulticastSocket socket;
\\initialise socket..
while (running) {
try {
    synchronized (syncRoot) {
        socket.setSoTimeout(WAIT_TIME);
        socket.receive(recv);
        //Do Something...
    }
} catch (SocketTimeoutException e) {
}

当前,当我的计算机从Windows睡眠模式唤醒时,如果我没有发送数据包,它将不断抛出套接字异常. 我已经检查了变量socket.isBound(),socket.isClosed(),socket.isConnected(),从工作时起它们没有发生变化.我是否缺少变量? 即使套接字在工作,它也会返回isConnected()= false,isBound()= true,isClosed()= false.

Currently when my computer wakes up from windows sleep mode it continually throws a socket exception when I no their are packets being sent. I have checked variables socket.isBound(), socket.isClosed(), socket.isConnected() and they have not changed from when it was working. Am I missing a variable? Even when the socket is working it returns isConnected() = false, isBound() = true, isClosed() = false.

如果我得到10,然后重新初始化我的多播套接字,我是否需要做一些事情计算SocketTimeoutExceptions的数量?

Do I need to do something like count the number of SocketTimeoutExceptions if i get 10 then reinitialise my multicast socket?

推荐答案

因此,在我的mutlicast套接字发送和接收消息时,我决定在其中检查一下(如果您没有在10秒内收到刚刚发送的数据包),然后重新启动套接字连接.效果很好.

So as my mutlicast socket sends and receives I decided to put a check in there if you do not receive a packet that you have just sent within 10seconds then restart the socket connection. This worked fine.

更新答案:2012/09/06

EJB没有连接,但是当Windows进入睡眠状态时,它会关闭网络适配器或某些设备(不是100%确定它在做什么,但是套接字会停止工作,但是所有代码值都表示它们仍然处于活动状态).但是,代码从睡眠状态启动时所执行的操作是,它认为其多播套接字仍处于连接状态,因此很高兴继续侦听但从未收到任何消息.实际上,它甚至很乐意在多路插座上发送数据而不会引发异常.

EJB as you said there is no connections but when windows goes to sleep it turns off the network adapter or something (not 100% sure what its doing but sockets stop working but all the code values say they are still active). But what the code does when it starts up from sleep is it thinks that its multicast socket is still connected, so it is happy to continue listening but never receives anything. Infact it is even happy sending data on the multisocket without throwing an exception.

因此,此修补程序并不适合所有人,但是当我在一个多路插座地址上发送和接收数据时,基本上,如果我在10秒钟内未收到发送的数据包,我会认为出了点问题并重新开始了连接.这是一段代码,解释了我是如何做到的:

So this fix is not perfect for everyone but as I was sending and receiving data on the one multisocket address, basically if I do not receive my sent packet back in 10 seconds I assume something has gone wrong and restart the connection. Here is a snippit of code that sort of explains how I did it:

MulticastSocket socket;
\\initialise socket..
while (running) {
try {
    synchronized (syncRoot) {
    if (sendMessagesQueue.size() > 0) {
        lastOutBoundMessage = sendMessagesQueue.remove();
        byte[] msg = lastOutBoundMessage.toByte();
        DatagramPacket outboundPacket = new DatagramPacket(
            msg, msg.length, group,
            socket.getLocalPort());
        synchronized (syncRoot) {
            socket.send(outboundPacket);
            lastSentMessage = DateTime.now();
        }

        socket.setSoTimeout(WAIT_TIME);
        socket.receive(recv);

        // Compare lastOutBoundMessage  and recv
        // if same set values to null
        // lastSentMessage = null;
        // lastOutBoundMessage = null;
    }
} catch (SocketTimeoutException e) {
    if (lastSentMessage != null && lastSentMessage.plusSeconds(10).isBeforeNow()) {
        running = false;
        // restart thread so connection will start again.
    }
}

这篇关于多播套接字-当计算机从睡眠中唤醒时将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆