Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main.java
- package d2ntserver;
- import java.net.*;
- import java.io.*;
- /**
- *
- * @author sataan1337
- */
- public class Main {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- try {
- Thread ts = new Server(666);
- System.out.println("Server läuft");
- }
- catch (IOException e){
- System.out.print(e);
- }
- }
- }
- //server.java
- package d2ntserver;
- import java.net.*;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- import DataClasses.*;
- /**
- *
- * @author sataan1337
- */
- public class Server extends Thread{
- ServerSocket server;
- public List<Thread> clients;
- public int port;
- public Server(int portx) throws IOException {
- port = portx;
- run();
- }
- public void run(){
- try{
- server = new ServerSocket(port);
- clients = new ArrayList<Thread>();
- while (true){
- // acceptNext();
- }
- }
- catch (Exception e){
- System.out.print(e);
- }
- }
- public void acceptNext() throws Exception{
- Socket client = server.accept();
- new ServerThread(this, client, clients.size()).run();
- //clients.add(new ServerThread(this, client, clients.size()));
- //clients.get(clients.size() -1).start();
- System.out.println("thread im hintergrund");
- }
- public void removeThread(Thread threadx){
- clients.remove(threadx);
- }
- }
- // serverthread.java
- package d2ntserver;
- import java.net.*;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- import DataClasses.*;
- import WorkerClasses.*;
- /**
- *
- * @author sataan1337
- */
- public class ServerThread extends Thread{
- public int connectionIndex;
- public SocketHelper socket;
- public Server server;
- public boolean done;
- public Database db;
- public String username;
- public String password;
- public boolean loggedin = false;
- Server serverxx;
- Socket clientxx;
- int indexx;
- public ServerThread(Server serverx, Socket clientx, int index) throws Exception{
- serverxx = serverx;
- clientxx = clientx;
- indexx = index;
- // loop();
- }
- public void loop() throws Exception{
- while (!done){
- try{
- String command = socket.receiveString();
- System.out.println("empfangen: " + command);
- evalCommand(command);
- }
- catch(IOException e){
- System.out.print(e);
- }
- }
- }
- public void evalCommand(String command) throws Exception{
- if (command.equals("login")){
- if (!loggedin) login();
- else socket.sendString("loginerror");
- }
- if (command.equals("echorequest")){
- socket.sendString("echoresponse");
- }
- }
- public void login() throws Exception{
- try{
- socket.sendString("username");
- String uname = socket.receiveString();
- socket.sendString("password");
- String pwd = socket.receiveString();
- if (db.login(uname, pwd)){
- socket.sendString("logindone");
- username = uname;
- password = pwd;
- loggedin = true;
- }
- else {
- socket.sendString("loginerror");
- }
- }
- catch(IOException e){
- }
- }
- public void run(){
- try{
- socket = new SocketHelper(clientxx);
- server = serverxx;
- connectionIndex = indexx;
- done = false;
- db = new Database();
- System.out.println("client connected");
- loop();
- }
- catch(Exception e){
- System.out.print(e);
- }
- }
- public void closeThread(){
- socket.closeAll();
- server.removeThread(this);
- done = true;
- }
- //constructor for tests
- public ServerThread(int port) throws IOException{
- ServerSocket srvr = new ServerSocket(port);
- try{
- socket = new SocketHelper(srvr.accept());
- }
- catch(Exception e){
- System.out.print(e);
- }
- done = false;
- db = new Database();
- System.out.println("client connected");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement