Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 6th, 2012  |  syntax: None  |  size: 0.89 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. private boolean setup()
  2.         {
  3.                 if(isInitialized)
  4.                         return true;
  5.  
  6.                 try
  7.                 {
  8.                         address = InetAddress.getByName(IP);
  9.        
  10.                         socket = new MulticastSocket(port);
  11.                         socket.setTimeToLive(TTL);
  12.                         socket.joinGroup(address);
  13.                         socket.setSoTimeout(1000);
  14.                 }
  15.                 catch(Exception e)
  16.                 {
  17.                         e.printStackTrace();
  18.                         return false;
  19.                 }
  20.  
  21.                 isInitialized = true;
  22.                 return true;
  23.         }
  24.  
  25.         public void run()
  26.         {      
  27.                 if(!setup())
  28.                 {
  29.                         // TODO : LOG ERROR (or something)
  30.                         return;
  31.                 }
  32.  
  33.                 isRunning = true;
  34.  
  35.                 int i = 0;
  36.                 while(isRunning)
  37.                 {
  38.                         try
  39.                         {
  40.                                 System.out.print("Receiving packet... ");
  41.                                 socket.receive(lastPacket);
  42.                                 System.out.println(++i);
  43.                         }
  44.                         catch(SocketTimeoutException e)
  45.                         {
  46.                                 // We want to keep listening forever
  47.                                 System.out.println("Timeout");
  48.                                 continue;
  49.                         }
  50.                         catch(Exception e)
  51.                         {
  52.                                 // TODO: Log the error
  53.                         }
  54.                 }
  55.  
  56.                 disconnect();
  57.         }