Advertisement
Guest User

Untitled

a guest
Dec 26th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. package server;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8.  
  9. import login.Login;
  10. import main.ControlPanel;
  11.  
  12. public class Server extends Thread {
  13.  
  14. final private ServerSocket m_serverSocket;
  15. final public static int MAX_CLIENTS = 3000;
  16. final private static SubServer[] m_clientConnections = new SubServer[MAX_CLIENTS];
  17. public static DataOutputStream socketOut;
  18. public static DataInputStream socketIn;
  19. private Socket connection;
  20.  
  21. public Server(int port) throws IOException {
  22. new ControlPanel();
  23. this.m_serverSocket = new ServerSocket(port);
  24. start();
  25. }
  26.  
  27. @Override
  28. public void run() {
  29. while (!interrupted()) {
  30. try {
  31. connection = this.m_serverSocket.accept();
  32. socketOut = new DataOutputStream(connection.getOutputStream());
  33. socketIn = new DataInputStream(connection.getInputStream());
  34. assignConnectionToSubServer(connection);
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40.  
  41. public void assignConnectionToSubServer(Socket connection) {
  42. for (int i = 0; i < MAX_CLIENTS; i++) {
  43. if (m_clientConnections[i] == null) {
  44. m_clientConnections[i] = new SubServer(connection, i);
  45. break;
  46. }
  47. }
  48. }
  49.  
  50. public static boolean kick(String user) {
  51. m_clientConnections[1].close();
  52. return true;
  53. }
  54.  
  55. protected class SubServer extends Thread {
  56.  
  57. final private int m_id;
  58. final private Socket m_connection;
  59. private String username;
  60. private String password;
  61.  
  62. public SubServer(Socket connection, int id) {
  63. this.m_id = id;
  64. this.m_connection = connection;
  65. start();
  66. }
  67.  
  68. @Override
  69. public void run() {
  70. while (!this.interrupted()) {
  71. try {
  72. username = socketIn.readUTF();
  73. password = socketIn.readUTF();
  74. Login.login(username, password);
  75.  
  76. int i = socketIn.readInt();
  77. System.out.println(i);
  78. if (i == 1) {
  79. ControlPanel.model.addElement(username);
  80. }
  81.  
  82. System.out.println(connection.getRemoteSocketAddress()
  83. + " connected\n");
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89.  
  90. public void process(String message) {
  91. }
  92.  
  93. public void close() {
  94. try {
  95. this.m_connection.close();
  96. } catch (IOException e) {
  97. }
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement