- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiCastSender.java
More file actions
Latest commit
59 lines (43 loc) · 1.63 KB
/
Copy pathMultiCastSender.java
File metadata and controls
59 lines (43 loc) · 1.63 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
//MultiCast is to send a message to a group of people
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.ObjectOutput;
importjava.io.ObjectOutputStream;
importjava.net.DatagramPacket;
importjava.net.InetAddress;
importjava.net.MulticastSocket;
publicclassMultiCastSender {
publicstaticvoidmain(String[] args) {
try {
InetAddressgroup = InetAddress.getByName("225.4.5.6");
MulticastSocketmulticastSock = newMulticastSocket();
Mensajemsg = newMensaje("Hola desde el Sender, como estas ?", 9);
//byte[] yourBytes = (byte[]) msg.getInSendingForm();
System.out.println(msg);
ByteArrayOutputStreambos = newByteArrayOutputStream();
ObjectOutputout = null;
byte[] yourBytes =null;
try {
out = newObjectOutputStream(bos);
System.out.println("holaa 1");
out.writeObject(msg);
System.out.println("holaa 2");
out.flush();
System.out.println("holaa 3");
yourBytes = bos.toByteArray();
System.out.println("holaa 4");
}finally {
try {
bos.close();
} catch (IOExceptionex) {
// ignore close exception
}
}
DatagramPacketpacket = newDatagramPacket(yourBytes, yourBytes.length, group, 3456);
multicastSock.send(packet);
multicastSock.close();
} catch (Exceptione) {
e.printStackTrace();
}
}
}