Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 3rd, 2012  |  syntax: Java  |  size: 2.24 KB  |  views: 54  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package udps;
  2.  
  3. import java.io.IOException;
  4. import java.net.DatagramPacket;
  5. import java.net.DatagramSocket;
  6. import java.net.SocketException;
  7. import java.util.concurrent.ExecutorService;
  8. import java.util.concurrent.Executors;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. public class UDPServer {
  13.  
  14.     public static void main(String[] args) {
  15.         UDPServer server = new UDPServer();
  16.         server.init();
  17.     }
  18.    
  19.     public void init() {
  20.         Thread serverThread = new Thread(new Server());
  21.         serverThread.start();
  22.     }
  23.  
  24.     public class Server implements Runnable {
  25.  
  26.         public void run() {
  27.             DatagramSocket socket = null;
  28.            
  29.             try {
  30.                 socket = new DatagramSocket(7777);
  31.             } catch (SocketException ex) {
  32.                 Logger.getLogger(UDPServer.class.getName()).log(Level.SEVERE, null, ex);
  33.             }
  34.            
  35.             ExecutorService pool = Executors.newFixedThreadPool(32);
  36.  
  37.             for (;;) {
  38.                 DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
  39.                
  40.                 try {
  41.                     socket.receive(packet);
  42.                 } catch (IOException ex) {
  43.                     Logger.getLogger(UDPServer.class.getName()).log(Level.SEVERE, null, ex);
  44.                 }
  45.                
  46.                 pool.execute(new Responder(socket, packet));
  47.             }
  48.         }
  49.     }
  50.  
  51.     public class Responder implements Runnable {
  52.  
  53.         DatagramSocket socket = null;
  54.         DatagramPacket packet = null;
  55.  
  56.         public Responder(DatagramSocket socket, DatagramPacket packet) {
  57.             this.socket = socket;
  58.             this.packet = packet;
  59.         }
  60.  
  61.         public void run() {
  62.             byte[] data = "PONG".getBytes();
  63.            
  64.             DatagramPacket response = new DatagramPacket(data, data.length, packet.getAddress(), packet.getPort());
  65.            
  66.             System.out.println(packet.getData().toString());
  67.            
  68.             try {
  69.                 socket.send(response);
  70.             } catch (IOException ex) {
  71.                 Logger.getLogger(UDPServer.class.getName()).log(Level.SEVERE, null, ex);
  72.             }
  73.         }
  74.     }
  75. }