Example usage for java.net MulticastSocket getLocalSocketAddress

List of usage examples for java.net MulticastSocket getLocalSocketAddress

Introduction

In this page you can find the example usage for java.net MulticastSocket getLocalSocketAddress.

Prototype


public SocketAddress getLocalSocketAddress() 

Source Link

Document

Returns the address of the endpoint this socket is bound to.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    int mcPort = 12345;
    String mcIPStr = "230.1.1.1";
    MulticastSocket mcSocket = null;
    InetAddress mcIPAddress = null;
    mcIPAddress = InetAddress.getByName(mcIPStr);
    mcSocket = new MulticastSocket(mcPort);
    System.out.println("Multicast Receiver running at:" + mcSocket.getLocalSocketAddress());
    mcSocket.joinGroup(mcIPAddress);/*  www .j a  va2 s . c o  m*/

    DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);

    System.out.println("Waiting for a  multicast message...");
    mcSocket.receive(packet);
    String msg = new String(packet.getData(), packet.getOffset(), packet.getLength());
    System.out.println("[Multicast  Receiver] Received:" + msg);

    mcSocket.leaveGroup(mcIPAddress);
    mcSocket.close();
}