Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 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. String privMsg ="private";
  62. if(input.startsWith(privMsg)){
  63. int ID_SendTo = Integer.parseInt(input.substring(privMsg.length(),privMsg.length()+5));
  64.  
  65. String msg = input.substring(privMsg.length()+6);
  66. if(findClient(ID_SendTo)!=-1){
  67. clients[findClient(ID_SendTo)].send(privMsg + "from " + ID + ":" +msg);
  68. }
  69. else{
  70. clients[findClient(ID)].send("user: " + ID_SendTo+ "was not found");
  71. }
  72.  
  73. }
  74. else {
  75.  
  76. System.out.println("Message from " + ID+ ":"+ input);
  77. for(int i=0; i<clientCount; i++){
  78. //add line of code to print the user's message
  79. //on the server side for spying
  80. clients[i].send("User: "+ ID + ": "+input);
  81. }
  82. }
  83. if(input.equalsIgnoreCase("bye")){
  84. remove(ID);//person said bye so remove them
  85. }
  86. }
  87.  
  88. public synchronized void remove(int ID){
  89. int position = findClient(ID);
  90. if(position >=0){
  91. ChatServerThread toRemove = clients[position];
  92. if(position <clientCount-1){
  93. for(int i= position+1; i <clientCount; i++){
  94. clients[i-1] = clients[i];
  95. }
  96. clientCount--;
  97. }
  98. try {
  99. toRemove.close();//close the person's that said bye connection
  100. } catch (IOException e) {
  101. // TODO Auto-generated catch block
  102. e.printStackTrace();
  103. }
  104. }
  105.  
  106. }
  107. private int findClient(int ID){
  108. for(int i=0; i<clientCount; i++){
  109. if(clients[i].getID() == ID){
  110. return i;
  111. }
  112. }
  113. return -1;//not in the array
  114. }
  115.  
  116. public static void main(String [] args){
  117. ChatServer myServer = null;
  118. if(args.length !=1){
  119. System.out.println("You need to specify a port number!!!");
  120. }
  121. else{
  122. int portNum = Integer.parseInt(args[0]);
  123. myServer = new ChatServer(portNum);//create an instance of my ChatServer
  124. }
  125. }
  126.  
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement