Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1.  
  2. import java.io.IOException;
  3. import java.net.ServerSocket;
  4. import java.net.Socket;
  5. import java.util.concurrent.*;
  6.  
  7. public class TCPServer {
  8.  
  9. public static int queueSize = 10;
  10. public static int minimumPoolSize = 1;
  11. public static int startPoolSize = 2;
  12. static Socket client;
  13. static ServerSocket ss;
  14. static ExecutorService executor;
  15. private static int port = 8081;
  16. public int ClientCount = 1;
  17.  
  18. public TCPServer(int startPoolSize, int port) {
  19. final BlockingQueue<Runnable> queue = new ArrayBlockingQueue(queueSize);
  20. executor = new ThreadPoolExecutor(minimumPoolSize, startPoolSize, 0L, TimeUnit.MILLISECONDS, queue);
  21. }
  22.  
  23.  
  24.  
  25. public static void main(String[] args) {
  26.  
  27. TCPServer tserver = new TCPServer(startPoolSize, port);
  28. try {
  29. tserver.startserver();
  30. }catch(IOException e){
  31. e.printStackTrace();
  32. }
  33.  
  34. }
  35.  
  36. public void startserver() throws IOException{
  37. ss = new ServerSocket(port);
  38. System.out.println("Listening to: " + port);
  39.  
  40. while (true){
  41. client = ss.accept();
  42. System.out.println("Client " + ClientCount + " Connected");
  43. executor.execute(new ClientThread(client, ClientCount));
  44. ClientCount++;
  45. }
  46. }
  47.  
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement