0

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?

user1434177
  • 1,947
  • 4
  • 26
  • 40
  • It is not a Java problem. Are you intending to wake up a network interface (power-managed network device) from its sleep? When a WOL-device is sleeping, it becomes passive, listens and sends nothing. You need to send a special magic packet to wake it up if its Magic Packet feature is enabled. You need Jpcap to do this low-level networking (or there is other Java solution). More about it here http://www.activexperts.com/activsocket/tutorials/wol/ If you don't like this WOL-feature, just turn it off for the network device so that the device can still work while the computer sleeps. – ee. Aug 14 '12 at 02:18
  • Ok, there is other Java solution (without using Jpcap) to send Magic Packet to wake up a LAN device http://www.jibble.org/wake-on-lan/ – ee. Aug 14 '12 at 02:24
  • Continually throws what exception? From what line of code? – user207421 Aug 14 '12 at 22:55

1 Answers1

0

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.
    }
}
user1434177
  • 1,947
  • 4
  • 26
  • 40
  • There are no connections in multicast. Please explain what you actually did. – user207421 Aug 14 '12 at 22:56
  • I closed this since you solved your problem, but there's no real detail as to (1) what the problem was or (2) how you actually solved it, leaving this rather ambiguous. If you'd like to elaborate on your answer a bit more (and perhaps the question, noting comments received regarding exceptions) I'd be more than happy to reopen this. – Tim Post Aug 15 '12 at 01:24
  • There are connections in an IGMP multicast socket. The router manages reservations for each IGMP multicast socket. If the socket doesn't broadcast a renewal, it gets removed from the group. – Robin Davies Dec 16 '12 at 01:11