Advertisement
Guest User

Server

a guest
Jun 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. package de.cloud.tammo.communication;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.util.HashMap;
  6.  
  7. /**
  8.  * Created by Tammo on 26.06.2017.
  9.  */
  10. public class Server {
  11.  
  12.     private int port;
  13.     private Thread t;
  14.     public ServerSocket serversocket;
  15.     private HashMap<String, Handler> handler;
  16.     private boolean running = false;
  17.  
  18.     public Server(int port) {
  19.         this.port = port;
  20.         handler = new HashMap<>();
  21.     }
  22.  
  23.     public void start() {
  24.         t = new Thread(new Runnable() {
  25.  
  26.             @Override
  27.             public void run() {
  28.                 try {
  29.                     serversocket = new ServerSocket(port);
  30.                     running = true;
  31.                     while (running) {
  32.                         Socket s = serversocket.accept();
  33.                         ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
  34.                         try {
  35.                             Packet p = (Packet) ois.readObject();
  36.                             handler.get(p.getName()).handle(p, s);
  37.                         } catch (ClassNotFoundException e) {
  38.                             e.printStackTrace();
  39.                         }
  40.                     }
  41.                 } catch (IOException e) {
  42.                     e.printStackTrace();
  43.                 }
  44.             }
  45.         });
  46.         t.start();
  47.     }
  48.  
  49.     public void stop() {
  50.         running = false;
  51.         try {
  52.             serversocket.close();
  53.         } catch (IOException e) {
  54.             e.printStackTrace();
  55.         }
  56.         t.interrupt();
  57.     }
  58.  
  59.     public void sendPacketWithTryCatch(Packet p, String host, int port) throws IOException, ConnectException {
  60.         Socket s = new Socket(host, port);
  61.         ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
  62.         oos.writeObject(p);
  63.         s.close();
  64.     }
  65.  
  66.     public void sendPacket(Packet p, String host, int port){
  67.         try {
  68.             Socket s = new Socket(host, port);
  69.             ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
  70.             oos.writeObject(p);
  71.             s.close();
  72.         } catch (IOException e) {
  73.             e.printStackTrace();
  74.         }
  75.     }
  76.  
  77.     public void addHandler(String packetname, Handler h) {
  78.         this.handler.put(packetname, h);
  79.     }
  80.  
  81.     public HashMap<String, Handler> getHandler() {
  82.         return handler;
  83.     }
  84.  
  85.     public int getPort() {
  86.         return port;
  87.     }
  88.  
  89.     public void setPort(int port) {
  90.         this.port = port;
  91.     }
  92.  
  93.     public boolean getRunningState() {
  94.         return this.running;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement