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