VasilM

srv

May 29th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class MultiEchoServer {
  5.     public static void main(String[] args) {
  6.         try {
  7.             ServerSocket s = new ServerSocket(4444);
  8.             while (true) {
  9.                 Socket incoming = s.accept();
  10.                 new ClientHandler(incoming).start();
  11.             }
  12.         } catch (Exception e) {}
  13.     }
  14. }
  15.  
  16. class ClientHandler extends Thread {
  17.     protected Socket incoming;
  18.     public ClientHandler(Socket incoming) {
  19.         this.incoming = incoming;
  20.     }
  21.     public void run() {
  22.         try {
  23.             BufferedReader in
  24.                 = new BufferedReader(
  25.                     new InputStreamReader(
  26.                         incoming.getInputStream()));
  27.             PrintWriter out
  28.                 = new PrintWriter(
  29.                     new OutputStreamWriter(
  30.                         incoming.getOutputStream()));
  31.             out.println("Hello! ...");
  32.             out.println("Enter BYE to exit.");
  33.             out.flush();
  34.             while (true) {
  35.                 String str = in.readLine();
  36.                 if (str == null) {
  37.                     break;
  38.                 } else {
  39.                     out.println("Echo: " + str);
  40.                     out.flush();
  41.                     if (str.trim().equals("BYE"))
  42.                         break;
  43.                     }
  44.             }
  45.             incoming.close();
  46.         } catch (IOException e) {}
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment