Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1.  
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintStream;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.util.Scanner;
  10. import java.util.concurrent.ExecutorService;
  11. import java.util.concurrent.Executors;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. /**
  16. *
  17. * @author SHUBHAM
  18. */
  19. public class Server {
  20.  
  21. int port;
  22. ServerSocket server=null;
  23. Socket client=null;
  24. ExecutorService pool = null;
  25. int clientcount=0;
  26.  
  27. public static void main(String[] args) throws IOException {
  28. Server serverobj=new Server(4546);
  29. serverobj.startServer();
  30. }
  31.  
  32. Server(int port){
  33. this.port=port;
  34. pool = Executors.newFixedThreadPool(5);
  35. }
  36.  
  37. public void startServer() throws IOException {
  38.  
  39. server=new ServerSocket(4546);
  40. System.out.println("Server Booted");
  41. System.out.println("Any client can stop the server by sending -1");
  42. while(true)
  43. {
  44. client=server.accept();
  45. clientcount++;
  46. ServerThread runnable= new ServerThread(client,clientcount,this);
  47. pool.execute(runnable);
  48. }
  49.  
  50. }
  51.  
  52. private static class ServerThread implements Runnable {
  53.  
  54. Server server=null;
  55. Socket client=null;
  56. BufferedReader cin;
  57. PrintStream cout;
  58. Scanner sc=new Scanner(System.in);
  59. int id;
  60. String s;
  61.  
  62. ServerThread(Socket client, int count ,Server server ) throws IOException {
  63.  
  64. this.client=client;
  65. this.server=server;
  66. this.id=count;
  67. System.out.println("Connection "+id+"established with client "+client);
  68.  
  69. cin=new BufferedReader(new InputStreamReader(client.getInputStream()));
  70. cout=new PrintStream(client.getOutputStream());
  71.  
  72. }
  73.  
  74. @Override
  75. public void run() {
  76. int x=1;
  77. try{
  78. while(true){
  79. s=cin.readLine();
  80.  
  81. System. out.print("Client("+id+") :"+s+"\n");
  82. System.out.print("Server : ");
  83. //s=stdin.readLine();
  84. s=sc.nextLine();
  85. if (s.equalsIgnoreCase("bye"))
  86. {
  87. cout.println("BYE");
  88. x=0;
  89. System.out.println("Connection ended by server");
  90. break;
  91. }
  92. cout.println(s);
  93. }
  94.  
  95.  
  96. cin.close();
  97. client.close();
  98. cout.close();
  99. if(x==0) {
  100. System.out.println( "Server cleaning up." );
  101. System.exit(0);
  102. }
  103. }
  104. catch(IOException ex){
  105. System.out.println("Error : "+ex);
  106. }
  107.  
  108.  
  109. }
  110. }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement