PJF16

Server + Thread

Jan 22nd, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.45 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6.  
  7. import java.net.*;
  8. import java.io.*;
  9.  
  10. public class ChatServerThread extends Thread
  11. {
  12.  
  13.     private ChatServer server = null;
  14.     private Socket socket = null;
  15.     private int ID = -1;
  16.     private BufferedReader streamIn = null;
  17.     private PrintWriter streamOut = null;
  18.  
  19.     public ChatServerThread(ChatServer _server, Socket _socket)
  20.     {
  21.         super();
  22.         server = _server;
  23.         socket = _socket;
  24.         ID = socket.getPort();
  25.     }
  26.  
  27.     public void send(String msg)
  28.     {
  29.         streamOut.println(msg);
  30.     }
  31.  
  32.     public int getID()
  33.     {
  34.         return ID;
  35.     }
  36.  
  37.     @Override
  38.     public void run()
  39.     {
  40.         System.out.println("Server Thread " + ID + " running.");
  41.         while (true && !this.isInterrupted())
  42.         {
  43.             try
  44.             {
  45.                 server.handle(ID, streamIn.readLine());
  46.             } catch (IOException ioe)
  47.             {
  48.                 System.out.println(ID + " ERROR reading: " + ioe.getMessage());
  49.                 server.remove(ID);
  50.                 this.interrupt();
  51.             }
  52.         }
  53.     }
  54.  
  55.     public void open() throws IOException
  56.     {
  57.         streamIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  58.         streamOut = new PrintWriter(socket.getOutputStream(), true);
  59.     }
  60.  
  61.    
  62.     public void close() throws IOException
  63.     {
  64.         if (socket != null)
  65.         {
  66.             socket.close();
  67.         }
  68.         if (streamIn != null)
  69.         {
  70.             streamIn.close();
  71.         }
  72.         if (streamOut != null)
  73.         {
  74.             streamOut.close();
  75.         }
  76.     }
  77. }
  78.  
  79. /*
  80.  * To change this template, choose Tools | Templates
  81.  * and open the template in the editor.
  82.  */
  83.  
  84.  
  85. import java.net.*;
  86. import java.io.*;
  87.  
  88. public class ChatServer implements Runnable
  89. {
  90.  
  91.     private ChatServerThread clients[] = new ChatServerThread[50];
  92.     private ServerSocket server = null;
  93.     private Thread thread = null;
  94.     private int clientCount = 0;
  95.  
  96.     public ChatServer(int port)
  97.     {
  98.         try
  99.         {
  100.             System.out.println("Binding to port " + port + ", please wait  ...");
  101.             server = new ServerSocket(port);
  102.             System.out.println("Server started: " + server);
  103.             start();
  104.         } catch (IOException ioe)
  105.         {
  106.             System.out.println("Can not bind to port " + port + ": " + ioe.getMessage());
  107.         }
  108.     }
  109.  
  110.     @Override
  111.     public void run()
  112.     {
  113.         while (thread != null && !thread.isInterrupted())
  114.         {
  115.             try
  116.             {
  117.                 System.out.println("Waiting for a client ...");
  118.                 addThread(server.accept());
  119.             } catch (IOException ioe)
  120.             {
  121.                 System.out.println("Server accept error: " + ioe);
  122.                 stop();
  123.             }
  124.         }
  125.     }
  126.  
  127.     private int findClient(int ID)
  128.     {
  129.         for (int i = 0; i < clientCount; i++)
  130.         {
  131.             if (clients[i].getID() == ID)
  132.             {
  133.                 return i;
  134.             }
  135.         }
  136.         return -1;
  137.     }
  138.  
  139.     public synchronized void handle(int ID, String input)
  140.     {
  141.         if (input.equals(".bye"))
  142.         {
  143.             clients[findClient(ID)].send(".bye");
  144.             remove(ID);
  145.         } else
  146.         {
  147.             for (int i = 0; i < clientCount; i++)
  148.             {
  149.                 clients[i].send(ID + ": " + input);
  150.             }
  151.         }
  152.     }
  153.  
  154.     public synchronized void remove(int ID)
  155.     {
  156.         int pos = findClient(ID);
  157.         if (pos >= 0)
  158.         {
  159.             ChatServerThread toTerminate = clients[pos];
  160.             System.out.println("Removing client thread " + ID + " at " + pos);
  161.             if (pos < clientCount - 1)
  162.             {
  163.                 for (int i = pos + 1; i < clientCount; i++)
  164.                 {
  165.                     clients[i - 1] = clients[i];
  166.                 }
  167.             }
  168.             clientCount--;
  169.             try
  170.             {
  171.                 toTerminate.close();
  172.             } catch (IOException ioe)
  173.             {
  174.                 System.out.println("Error closing thread: " + ioe);
  175.             }
  176.             toTerminate.interrupt();
  177.         }
  178.     }
  179.  
  180.     private void addThread(Socket socket)
  181.     {
  182.         if (clientCount < clients.length)
  183.         {
  184.             System.out.println("Client accepted: " + socket);
  185.             clients[clientCount] = new ChatServerThread(this, socket);
  186.             try
  187.             {
  188.                 clients[clientCount].open();
  189.                 clients[clientCount].start();
  190.                 clientCount++;
  191.             } catch (IOException ioe)
  192.             {
  193.                 System.out.println("Error opening thread: " + ioe);
  194.             }
  195.         } else
  196.         {
  197.             System.out.println("Client refused: maximum " + clients.length + " reached.");
  198.         }
  199.     }
  200.  
  201.     public void start()
  202.     {
  203.         if (thread == null)
  204.         {
  205.             thread = new Thread(this);
  206.             thread.start();
  207.         }
  208.     }
  209.  
  210.     public void stop()
  211.     {
  212.         if (thread != null)
  213.         {
  214.             thread.interrupt();
  215.             thread = null;
  216.         }
  217.     }
  218.  
  219.     public static void main(String args[])
  220.     {
  221.         ChatServer server = null;
  222.        
  223.         server = new ChatServer(9988);
  224.        
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment