A UDP Datagram Socket, a Multicast Sender Application - Java Network

Java examples for Network:UDP

Description

A UDP Datagram Socket, a Multicast Sender Application

Demo Code

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Main {
  public static void main(String[] args) throws Exception {
    DatagramSocket udpSocket = null;
    udpSocket = new DatagramSocket();
    InetAddress mcIPAddress = InetAddress.getByName("127.0.0.1");

    byte[] msg = "Hello multicast socket".getBytes();
    DatagramPacket packet = new DatagramPacket(msg, msg.length);
    packet.setAddress(mcIPAddress);//from w  ww.  j  ava  2  s.c o  m
    packet.setPort(5555);
    udpSocket.send(packet);

    System.out.println("Sent a multicast message.");
    System.out.println("Exiting application");
    udpSocket.close();
  }
}

Result


Related Tutorials