1
votes

how do I stop this method from executing?

multSocket.receive(multPacket);    

I'm trying to connect two or more machines (or applications on the same machine) via UDP Multicast with Java. The thing is I'm not working with client-server but more like a distributed object, so there isn't really a distinction between clients and servers.

Ideally the first application opened would start sending multicast messages every x seconds, and when the other one gets started would call a method for receiving the message or packet. But since each application can be client or server it's tricky to know which one to put to send and the other to receive.

My current idea for fixing this is to make each application send the multicast packet for 10 seconds, if no one responds then listen to multicast packets for 10 seconds, if it doesn't receive anything start sending again and keep the cycle. When one of them finally receives the message, send a confirmation to the sender and both of them can stop listening and sending.

But my problem is the receiving method. It stays listening for incoming multicast packets and I don't know how to stop or pause it.

This is how I init the socket just in case

MulticastSocket multSocket = new MulticastSocket(5001);
multSocket.joinGroup(230.0.0.1);
private byte[] multBuf = new byte[256];
DatagramPacket multPacket = new DatagramPacket(multBuf, multBuf.length);

Thanks for hearing me out.

-------------------------------------------------------------- EDIT --------------------------------------------------------------

-------------------------------------------------------------- EDIT --------------------------------------------------------------

@EJP Thanks for your answer! This is how I implemented the setSoTimeout on the receive multicast method:

public void receiveMulticast() throws IOException{

    multAddress = InetAddress.getByName("230.0.0.1");
    multSocket = new MulticastSocket(multPort);
    multSocket.joinGroup(multAddress);
    multBuf = new byte[256];
    multPacket = new DatagramPacket(multBuf, multBuf.length);

    multSocket.setSoTimeout(10000);

    try {
        // This is the sentence I had trouble with
        multSocket.receive(multPacket);

        receivedPacket = true;
        System.out.println("Received something");

    } catch (SocketTimeoutException s) {
        System.out.println("Socket timed out!");
    }

}

Here's more of the code in case someone's curious:

This is my send multicast method which uses sleep for sending every 10 seconds:

public void sendMulticast() throws IOException, InterruptedException{

    multAddress = InetAddress.getByName("230.0.0.1");
    multSocket = new MulticastSocket();
    multSocket.joinGroup(multAddress);
    int tcpPort = 5001;
    String data = String.valueOf(tcpPort);
    multBuf = data.getBytes();
    multPacket = new DatagramPacket(multBuf, multBuf.length, multAddress, multPort);

    int count = 0;
    while (count < 10) {
        Thread.sleep(1000);
        multSocket.send(multPacket);
        count++;
    }

}

And this is where I call those two methods infinitetly (on another class):

messageThread = new Thread() {
    @Override
    public void run() {
        while(true){
            try {
                stateLabel.setText("Sending");
                socket.sendMulticast();
                stateLabel.setText("Listening");
                socket.receiveMulticast();
            } catch (IOException ex) {
                Logger.getLogger(NumerosAmigosView.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex) {
                Logger.getLogger(NumerosAmigosView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
};
messageThread.start();

So the result is the program runs infinitely sending a packet for 10 seconds, receiving for 10 seconds and starting over. When I have two programs running at the same time, the packets get received :).

1

1 Answers

1
votes

Set a read timeout with setSoTimeout(), and catch the SocketTimeoutException that is thrown.