Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4.  
  5.  
  6. public class ChatServer implements Runnable {
  7.  
  8. private int clientCount =0;
  9. private ChatServerThread clients[] = new ChatServerThread[50];
  10. private ServerSocket server = null;
  11. Thread thread = null;
  12.  
  13. //same as version3
  14. public ChatServer(int port){
  15. try{
  16. server = new ServerSocket(port);//step1
  17. System.out.println("Started the server...waiting for a client");
  18. start(); //the chatserver's start method that goes ahead and creates a new thread
  19. }
  20. catch(IOException e){
  21. System.err.println("ERROR "+e.getMessage());
  22.  
  23. }
  24. }
  25.  
  26. public void start(){
  27. if(thread == null){
  28. thread = new Thread(this);
  29. thread.start();
  30. }
  31. }
  32.  
  33. @Override
  34. public void run() {//same as version 3
  35. while(thread !=null){
  36. try{
  37. System.out.println("Waiting for a client...");
  38. //now we add a new Thread and accept a client
  39. addThread(server.accept());
  40. } catch (IOException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. }
  45. }
  46.  
  47. public void addThread(Socket socket){
  48. if(clientCount < clients.length){
  49. clients[clientCount] = new ChatServerThread(this, socket);
  50. try {
  51. clients[clientCount].open();//open the stream for the ChatServerThread client
  52. clients[clientCount].start();//start to run the ChatServerThread client
  53. clientCount++;
  54. } catch (IOException e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60. public synchronized void handle(int ID, String input){
  61.  
  62. for(int i=0; i<clientCount; i++){
  63. //add line of code to print the user's message
  64. //on the server side for spying
  65. clients[i].send("User: "+ ID + ": "+input);
  66. System.out.println("User: "+ ID + ": "+input);
  67. }
  68. if(input.equalsIgnoreCase("bye")){
  69. remove(ID);//person said bye so remove them
  70. }
  71. }
  72.  
  73. public synchronized void remove(int ID){
  74. int position = findClient(ID);
  75. if(position >=0){
  76. ChatServerThread toRemove = clients[position];
  77. if(position <clientCount-1){
  78. for(int i= position+1; i <clientCount; i++){
  79. clients[i-1] = clients[i];
  80. }
  81. clientCount--;
  82. }
  83. try {
  84. toRemove.close();//close the person's that said bye connection
  85. } catch (IOException e) {
  86. // TODO Auto-generated catch block
  87. e.printStackTrace();
  88. }
  89. }
  90.  
  91. }
  92. private int findClient(int ID){
  93. for(int i=0; i<clientCount; i++){
  94. if(clients[i].getID() == ID){
  95. return i;
  96. }
  97. }
  98. return -1;//not in the array
  99. }
  100.  
  101. public static void main(String [] args){
  102. ChatServer myServer = null;
  103. if(args.length !=1){
  104. System.out.println("You need to specify a port number!!!");
  105. }
  106. else{
  107. int portNum = Integer.parseInt(args[0]);
  108. myServer = new ChatServer(portNum);//create an instance of my ChatServer
  109. }
  110. }
  111.  
  112.  
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement