Multicast Sniffer : TCP « Network Protocol « Java






Multicast Sniffer

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MulticastSniffer {

  public static void main(String[] args) {
  
    InetAddress ia = null;
    byte[] buffer = new byte[65509];
    DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
    int port = 0;
  
    try {
      try {
        ia = InetAddress.getByName(args[0]);
      }
      catch (UnknownHostException e)  {
        //
      }
      port = Integer.parseInt(args[1]);
    }  // end try
    catch (Exception e) {
      System.err.println(e);
      System.err.println("Usage: java MulticastSniffer MulticastAddress port");
      System.exit(1);
    }
  
    try {
      MulticastSocket ms = new MulticastSocket(port);
      ms.joinGroup(ia);
      while (true) {
        ms.receive(dp);
        String s = new String(dp.getData(), 0, 0, dp.getLength());
        System.out.println(s);
      }
    }
    catch (SocketException se) {
      System.err.println(se);
    }
    catch (IOException ie) {
      System.err.println(ie);
    }  
  
  }

}

           
       








Related examples in the same category

1.A tcp client, a tcp server, and a Serializable payload object which is sent from the server to the client
2.Use the Daytime TCP and Daytime UDP classes
3.Get Socket Information
4.Finger Socket
5.Multicast Sender
6.Connects to the default chargen service port
7.Connects to the default echo service portConnects to the default echo service port