Category Archives: DatagramSocket

WriteMulticast using the DatagramSocket.

Introduction

DatagramSocket represents a socket for sending and receiving datagram packets.
A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may be routed differently, and may arrive in any order.

The example below shows how a Multicast can be created using the DatagramSocket class.

byte[] outbuf = new byte[1024];
int port = 1234;
try {
    DatagramSocket socket = new DatagramSocket();
    InetAddress groupAddr = InetAddress.getByName("228.1.2.3");
    DatagramPacket packet = new DatagramPacket(outbuf, outbuf.length, groupAddr, port);
    socket.send(packet);
} catch (SocketException e) {
} catch (IOException e) {
}