-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMulticastSender.java
More file actions
41 lines (35 loc) · 1.14 KB
/
MulticastSender.java
File metadata and controls
41 lines (35 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.*;
import java.net.*;
public class MulticastSender {
public static void main(String[] args) {
InetAddress ia = null;
int port = 0;
byte ttl = (byte) 1;
// read the address from the command line
try {
ia = InetAddress.getByName(args[0]);
port = Integer.parseInt(args[1]);
if (args.length > 2) ttl = (byte) Integer.parseInt(args[2]);
} catch (NumberFormatException | IndexOutOfBoundsException
| UnknownHostException ex) {
System.err.println(ex);
System.err.println(
"Usage: java MulticastSender multicast_address port ttl");
System.exit(1);
}
byte[] data = "Here's some multicast data\r\n".getBytes();
DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);
try (MulticastSocket ms = new MulticastSocket()) {
ms.setTimeToLive(ttl);
ms.joinGroup(ia);
for (int i = 1; i < 10; i++) {
ms.send(dp);
}
ms.leaveGroup(ia);
} catch (SocketException ex) {
System.err.println(ex);
} catch (IOException ex) {
System.err.println(ex);
}
}
}