Java Network Programming


 


Simple Cleint/Server: Multi-Threaded Cleint/Server:
 
Example Cleint/Server:

 

Multicasting

Receiving multicast message

For 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.113

ms.joinGroup(group);
3. Use recive  to read the messages, e.g.,
DatagramPacket recv = new DatagramPacket(buf, buf.length);
ms.receive(recv);


Sendinging multicast message

For 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 Example

        Usage: java mchat <mgrp> <port>

        Example: java mchat  224.2.2.2   1234