1 |
package appl.parallel.test; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.net.DatagramPacket; |
5 |
import java.net.DatagramSocket; |
6 |
import java.net.InetAddress; |
7 |
import java.net.MulticastSocket; |
8 |
import java.net.UnknownHostException; |
9 |
|
10 |
public class MulticastSocketTest { |
11 |
public static void main( String[] args ) throws IOException |
12 |
{ |
13 |
|
14 |
String msg = "Hello"; |
15 |
InetAddress group = InetAddress.getByName("224.0.0.1"); |
16 |
MulticastSocket s = new MulticastSocket(50000); |
17 |
s.joinGroup(group); |
18 |
DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), |
19 |
group, 6789); |
20 |
System.out.println("sending"); |
21 |
s.send(hi); |
22 |
// get their responses! |
23 |
System.out.println("waiting"); |
24 |
byte[] buf = new byte[1000]; |
25 |
|
26 |
DatagramPacket recv = new DatagramPacket(buf, buf.length); |
27 |
s.receive(recv); |
28 |
System.out.println("finished"); |
29 |
// OK, I'm done talking - leave the group... |
30 |
s.leaveGroup(group); |
31 |
|
32 |
} |
33 |
|
34 |
} |
35 |
|