Sauron3

Untitled

Apr 20th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. package objects.comunication;
  2.  
  3. import java.io.IOException;
  4. import java.net.DatagramPacket;
  5. import java.net.DatagramSocket;
  6.  
  7. import communicator.Gui;
  8.  
  9. public class Server extends Thread {
  10.     private volatile boolean stop = false;
  11.     private String finalny;
  12.     private DatagramSocket sock;
  13.  
  14.     public void run() {
  15.         try {
  16.             // 1. creating a server socket, parameter is local port number
  17.             sock = new DatagramSocket(7777);
  18.  
  19.             // buffer to receive incoming data
  20.             byte[] buffer = new byte[20];
  21.             DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
  22.  
  23.             // communication loop
  24.             while (!stop) {
  25.                 sock.receive(incoming);
  26.                 byte[] data = incoming.getData();
  27.                 String s = new String(data, 0, incoming.getLength());
  28.  
  29.                 // echo the details of incoming data - client ip : client port -
  30.                 // client message
  31.                 finalny = (incoming.getAddress().getHostAddress() + " "
  32.                         + incoming.getPort() + " - " + s);
  33.                 Gui.getTextArea().append(finalny + "\n");
  34.             }
  35.         }
  36.  
  37.         catch (IOException e) {
  38.             System.err.println("IOException " + e);
  39.         }
  40.     }
  41.  
  42.     public void requestStop() {
  43.         stop = true;
  44.     }
  45.  
  46.     public String getFinalny() {
  47.         return finalny;
  48.     }
  49.  
  50.     public void setFinalny(String finalny) {
  51.         this.finalny = finalny;
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment