Advertisement
Guest User

Untitled

a guest
Jun 5th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.86 KB | None | 0 0
  1. //Main.java
  2. package d2ntserver;
  3.  
  4. import java.net.*;
  5. import java.io.*;
  6. /**
  7.  *
  8.  * @author sataan1337
  9.  */
  10. public class Main {
  11.  
  12.     /**
  13.      * @param args the command line arguments
  14.      */
  15.     public static void main(String[] args) {
  16.         try {
  17.             Thread ts = new Server(666);
  18.             System.out.println("Server läuft");
  19.         }
  20.         catch (IOException e){
  21.             System.out.print(e);
  22.         }
  23.     }
  24.  
  25. }
  26.  
  27.  
  28. //server.java
  29. package d2ntserver;
  30.  
  31. import java.net.*;
  32. import java.io.*;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import DataClasses.*;
  36. /**
  37.  *
  38.  * @author sataan1337
  39.  */
  40. public class Server extends Thread{
  41.     ServerSocket server;
  42.     public List<Thread> clients;
  43.     public int port;
  44.  
  45.  
  46.     public Server(int portx) throws IOException {
  47.         port = portx;
  48.         run();
  49.     }
  50.  
  51.     public void run(){
  52.         try{
  53.             server = new ServerSocket(port);
  54.             clients = new ArrayList<Thread>();
  55.             while (true){
  56.         //        acceptNext();
  57.             }
  58.         }
  59.         catch (Exception e){
  60.             System.out.print(e);
  61.         }
  62.     }
  63.  
  64.     public void acceptNext() throws Exception{
  65.         Socket client = server.accept();
  66.         new ServerThread(this, client, clients.size()).run();
  67.         //clients.add(new ServerThread(this, client, clients.size()));
  68.         //clients.get(clients.size() -1).start();
  69.         System.out.println("thread im hintergrund");
  70.     }
  71.  
  72.     public void removeThread(Thread threadx){
  73.         clients.remove(threadx);
  74.     }
  75.  
  76. }
  77.  
  78.  
  79. // serverthread.java
  80.  
  81.  
  82. package d2ntserver;
  83.  
  84. import java.net.*;
  85. import java.io.*;
  86. import java.util.ArrayList;
  87. import java.util.List;
  88. import DataClasses.*;
  89. import WorkerClasses.*;
  90.  
  91.  
  92. /**
  93.  *
  94.  * @author sataan1337
  95.  */
  96. public class ServerThread extends Thread{
  97.  
  98.    
  99.         public int connectionIndex;
  100.         public SocketHelper socket;
  101.  
  102.         public Server server;
  103.  
  104.         public boolean done;
  105.  
  106.         public Database db;
  107.  
  108.         public String username;
  109.         public String password;
  110.         public boolean loggedin = false;
  111.  
  112.         Server serverxx;
  113.         Socket clientxx;
  114.         int indexx;
  115.  
  116.         public ServerThread(Server serverx, Socket clientx, int index) throws Exception{
  117.             serverxx = serverx;
  118.             clientxx = clientx;
  119.             indexx = index;
  120.  
  121.       //      loop();
  122.         }
  123.  
  124.  
  125.         public void loop() throws Exception{
  126.             while (!done){
  127.                 try{
  128.                     String command = socket.receiveString();
  129.                     System.out.println("empfangen: " + command);
  130.                     evalCommand(command);
  131.                 }
  132.                 catch(IOException e){
  133.                     System.out.print(e);
  134.                 }
  135.             }
  136.         }
  137.  
  138.  
  139.  
  140.         public void evalCommand(String command) throws Exception{
  141.             if (command.equals("login")){
  142.                 if (!loggedin) login();
  143.                 else socket.sendString("loginerror");
  144.             }
  145.             if (command.equals("echorequest")){
  146.                 socket.sendString("echoresponse");
  147.             }
  148.         }
  149.  
  150.         public void login() throws Exception{
  151.             try{
  152.                 socket.sendString("username");
  153.                 String uname = socket.receiveString();
  154.                 socket.sendString("password");
  155.                 String pwd = socket.receiveString();
  156.                 if (db.login(uname, pwd)){
  157.                     socket.sendString("logindone");
  158.                     username = uname;
  159.                     password = pwd;
  160.                     loggedin = true;
  161.                 }
  162.                 else {
  163.                     socket.sendString("loginerror");
  164.                 }
  165.             }
  166.             catch(IOException e){
  167.  
  168.             }
  169.         }
  170.  
  171.  
  172.  
  173.         public void run(){
  174.             try{
  175.                 socket = new SocketHelper(clientxx);
  176.                 server = serverxx;
  177.                 connectionIndex = indexx;
  178.                 done = false;
  179.                 db = new Database();
  180.                 System.out.println("client connected");
  181.                 loop();
  182.             }
  183.             catch(Exception e){
  184.                 System.out.print(e);
  185.             }
  186.         }
  187.  
  188.         public void closeThread(){
  189.             socket.closeAll();
  190.  
  191.             server.removeThread(this);
  192.             done = true;
  193.         }
  194.  
  195.  
  196.        //constructor for tests
  197.         public ServerThread(int port) throws IOException{
  198.             ServerSocket srvr = new ServerSocket(port);
  199.             try{
  200.                 socket = new SocketHelper(srvr.accept());
  201.             }
  202.             catch(Exception e){
  203.                 System.out.print(e);
  204.             }
  205.             done = false;
  206.             db = new Database();
  207.  
  208.  
  209.             System.out.println("client connected");
  210.         }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement