multicast socket - will not work when computer wakes up from sleep [closed]

前提是你 提交于 2020-01-06 15:41:07

问题


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) {
}

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.

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


回答1:


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.

Updating answer: 2012/09/06

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.

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.
    }
}


来源:https://stackoverflow.com/questions/11944486/multicast-socket-will-not-work-when-computer-wakes-up-from-sleep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!