Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Timer;
- import java.util.TimerTask;
- import login.Login;
- public class Server extends Thread {
- private boolean running;
- private Socket connection;
- private ServerSocket serverSocket;
- public DataOutputStream socketOut;
- public DataInputStream socketIn;
- final private int MAX_CLIENTS = 3000;
- final private SubServer[] clientConnections = new SubServer[MAX_CLIENTS];
- final private static int port = 43594;
- private ControlPanel cp;
- public static void main(String[] args) throws IOException {
- new Server(port).createConnection();
- }
- public Server(int port) throws IOException {
- running = true;
- this.serverSocket = new ServerSocket(port);
- cp = new ControlPanel();
- load();
- start();
- }
- private void load() {
- DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
- Calendar cal = Calendar.getInstance();
- ControlPanel.append("[" + dateFormat.format(cal.getTime()) + "]:"
- + " Server is now online!");
- }
- private void createConnection() {
- try {
- connection = this.serverSocket.accept();
- socketOut = new DataOutputStream(connection.getOutputStream());
- socketIn = new DataInputStream(connection.getInputStream());
- assignConnectionToSubServer(connection);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void assignConnectionToSubServer(Socket connection) {
- for (int i = 0; i < MAX_CLIENTS; i++) {
- if (clientConnections[i] == null) {
- clientConnections[i] = new SubServer(connection, i, this,
- socketOut, socketIn);
- break;
- }
- }
- }
- public boolean kick(String username) {
- for (int i = 0; i < MAX_CLIENTS; i++) {
- try {
- if (clientConnections[i].getUsername().equals(username)) {
- clientConnections[i].close();
- clientConnections[i] = null;
- return true;
- }
- } catch (NullPointerException e) {
- }
- }
- return false;
- }
- }
- class SubServer extends Thread {
- final private int id;
- final private Socket connection;
- private String username;
- private String password;
- private Server server;
- private DataOutputStream socketOut;
- private DataInputStream socketIn;
- private BufferedReader br;
- public SubServer(Socket connection, int id, Server server,
- DataOutputStream socketOut, DataInputStream socketIn) {
- this.id = id;
- this.connection = connection;
- this.server = server;
- this.socketOut = socketOut;
- this.socketIn = socketIn;
- this.br = new BufferedReader(new InputStreamReader(socketIn));
- userLoop();
- start();
- }
- private void userLoop() { // checks for incoming data from client
- Timer t = new Timer();
- t.schedule(new TimerTask() {
- @Override
- public void run() {
- try {
- socketIn.read(); // check for heartbeat from client
- String userInput;
- while ((userInput = br.readLine()) != null) {
- }
- } catch (Exception e) {
- ControlPanel.model.removeElement(getUsername());
- ControlPanel.append(getUsername() + " has disconnected.");
- }
- }
- }, 1000);
- }
- public String getUsername() {
- return username;
- }
- public String getPassword() {
- return password;
- }
- public int getUserID() {
- return id;
- }
- @Override
- public void run() {
- while (!interrupted()) {
- try {
- username = socketIn.readUTF();
- password = socketIn.readUTF();
- Login.login(username, password, socketOut);
- int i = socketIn.readInt();
- if (i == 1) {
- ControlPanel.model.addElement(username);
- ControlPanel.append(getUsername() + " connected using "
- + connection.getRemoteSocketAddress());
- break;
- } else if (i == 0) {
- // do nothing, ignore it. client heartbeat
- break;
- }
- } catch (IOException e) {
- // e.printStackTrace();
- }
- }
- }
- public void close() {
- try {
- this.connection.close();
- } catch (IOException e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment