Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package server;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import login.Login;
- import main.ControlPanel;
- public class Server extends Thread {
- final private ServerSocket m_serverSocket;
- final public static int MAX_CLIENTS = 3000;
- final private static SubServer[] m_clientConnections = new SubServer[MAX_CLIENTS];
- public static DataOutputStream socketOut;
- public static DataInputStream socketIn;
- private Socket connection;
- public Server(int port) throws IOException {
- new ControlPanel();
- this.m_serverSocket = new ServerSocket(port);
- start();
- }
- @Override
- public void run() {
- while (!interrupted()) {
- try {
- connection = this.m_serverSocket.accept();
- socketOut = new DataOutputStream(connection.getOutputStream());
- socketIn = new DataInputStream(connection.getInputStream());
- assignConnectionToSubServer(connection);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void assignConnectionToSubServer(Socket connection) {
- for (int i = 0; i < MAX_CLIENTS; i++) {
- if (m_clientConnections[i] == null) {
- m_clientConnections[i] = new SubServer(connection, i);
- break;
- }
- }
- }
- public static boolean kick(String user) {
- m_clientConnections[1].close();
- return true;
- }
- protected class SubServer extends Thread {
- final private int m_id;
- final private Socket m_connection;
- private String username;
- private String password;
- public SubServer(Socket connection, int id) {
- this.m_id = id;
- this.m_connection = connection;
- start();
- }
- @Override
- public void run() {
- while (!this.interrupted()) {
- try {
- username = socketIn.readUTF();
- password = socketIn.readUTF();
- Login.login(username, password);
- int i = socketIn.readInt();
- System.out.println(i);
- if (i == 1) {
- ControlPanel.model.addElement(username);
- }
- System.out.println(connection.getRemoteSocketAddress()
- + " connected\n");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void process(String message) {
- }
- public void close() {
- try {
- this.m_connection.close();
- } catch (IOException e) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement