-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUDPMulticastClient.java
More file actions
65 lines (39 loc) · 1.65 KB
/
UDPMulticastClient.java
File metadata and controls
65 lines (39 loc) · 1.65 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package SocketProgram;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.SocketTimeoutException;
public class UDPMulticastClient {
public UDPMulticastClient() {
System.out.println("UDP Multicast client started");
try(MulticastSocket multicastSocket = new MulticastSocket(9877))
{
InetAddress inetAddress = InetAddress.getByName("228.5.6.7");
multicastSocket.joinGroup(inetAddress);
byte[] data = new byte[256];
DatagramPacket packet = new DatagramPacket(data, data.length);
while (true) {
System.out.println("Hi this is Client Waiting for server to send message:");
multicastSocket.receive(packet);
String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("Message from: " + packet.getAddress()
+ " Message: [" + message + "]");
}
}catch (SocketTimeoutException ex) {
System.out.println("Timeout error: " + ex.getMessage());
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Client error: " + ex.getMessage());
ex.printStackTrace();
} finally{
//if(multicastSocket != null){
// multicastSocket.close();
}
System.out.println("UDP Multicast client is terminating");
}
public static void main(String args[]) throws IOException
{
new UDPMulticastClient ();
}
}