Java Network Programming
%java JabberClient localhost
while(true) {
if (JabberClientThread.threadCount() < MAX_THREADS)
new JabberClientThread(addr);
Thread.currentThread().sleep(100);
}
2. Each client sends 25 messages to the server and exits.
for(int i = 0; i < 25;
i++) {
out.println("Client " + id + ": " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
%java MultiJabberClient localhost
(Type CTRL-C
to interrupt).
%java ChatterClient localhost
Multicasting
Receiving multicast messageFor a process to recieve multicast messages it needs to perform the following steps:
1. Craeate a UDP socket ms
MulticastSocket ms = new MulticastSocket(port);Bind ms it to port, e.g., 1234. All processes must bind to the same port in order to receive the multicast messages.2. Join a multicast group address group ,
e.g., 224.111.112.113ms.joinGroup(group);3. Use recive to read the messages, e.g.,DatagramPacket recv = new DatagramPacket(buf, buf.length);
ms.receive(recv);
Sendinging multicast messageFor a process to send multicast messages it needs to
perform the following:You may use the socket ms for sending multicast messages or you can use any other udp socket (it does not have to join any multicast group):
DatagramPacket dp = new DatagramPacket(buf, buf.length(), group, port);
ms.send(dp);
Time-to-live:
To control how far the messages can go,
e.g., 2 means at most 2 routers away.ms.setTimeToLive(2);Loopback Control:
To let the application get a copy of its own transmission:
ms.setLoopbackMode(true);
Multicast Examplemchat.java
Usage: java mchat <mgrp> <port>Example: java mchat 224.2.2.2 1234