HOME PCB
..MulticastHello.javaMulticastJsonObject.javaMulticastJsonReceivingThread.javaMulticastJsonSendingThread.javaMulticastObject.javaMulticastReceiveSingleObject.javaMulticastReceivingThread.javaMulticastSendingThread.javaMulticastThread.javaMulticastWebProgram.java
package Multicast;

import Common.Utils;

import java.io.IOException;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import java.util.ArrayList;

public abstract class MulticastThread extends Thread
{
    protected final int port;

    protected final String ip_group;

    protected MulticastSocket socket = null;

    protected InetAddress group = null;

    public MulticastThread(int port, String ip_group)
    {
        this.port = port;
        this.ip_group = ip_group;
    }

    public int getPort() {
        return this.port;
    }

    public String getIp_group() {
        return this.ip_group;
    }

    protected MulticastSocket getSocket() throws IOException
    {
        if (this.socket == null)
        {
            this.socket = new MulticastSocket(this.port);
            this.socket.joinGroup(this.getGroup());
        }
        return this.socket;
    }

    protected InetAddress getGroup() throws UnknownHostException
    {
        if (this.group == null)
        {
            this.group = InetAddress.getByName(this.ip_group);
        }
        return this.group;
    }

    protected void clearSocket() throws IOException
    {
        this.socket.leaveGroup(this.group);
        this.group = null;
        this.socket.close();
        System.gc();
    }
}