Advertisement
madhawaseeeee

tcp firefox

Sep 13th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. package socketcomm;
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5.  
  6. public class EchoServer2 extends Thread {
  7.  
  8.     protected Socket clientSocket;
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.         ServerSocket serverSocket = null;
  12.  
  13.         try {
  14.             serverSocket = new ServerSocket(5000);
  15.             System.out.println("Connection Socket Created");
  16.             try {
  17.                 while (true) {
  18.                     System.out.println("Waiting for Connection");
  19.                     new EchoServer2(serverSocket.accept());
  20.                 }
  21.             } catch (IOException e) {
  22.                 System.err.println("Accept failed.");
  23.                 System.exit(1);
  24.             }
  25.         } catch (IOException e) {
  26.             System.err.println("Could not listen on port: 5000.");
  27.             System.exit(1);
  28.         } finally {
  29.             try {
  30.                 serverSocket.close();
  31.             } catch (IOException e) {
  32.                 System.err.println("Could not close port: 5000.");
  33.                 System.exit(1);
  34.             }
  35.         }
  36.     }
  37.  
  38.     private EchoServer2(Socket clientSoc) {
  39.         clientSocket = clientSoc;
  40.         start();
  41.     }
  42.  
  43.     public void run() {
  44.         System.out.println("New Communication Thread Started");
  45.  
  46.         try {
  47.             PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
  48.                     true);
  49.             BufferedReader in = new BufferedReader(
  50.                     new InputStreamReader(clientSocket.getInputStream()));
  51.  
  52.             String inputLine;
  53.  
  54.             while ((inputLine = in.readLine()) != null) {
  55.                 System.out.println("Server: " + inputLine);
  56.                 out.println(inputLine);
  57.  
  58.                 if (inputLine.equals("Bye.")) {
  59.                     break;
  60.                 }
  61.             }
  62.  
  63.             out.close();
  64.             in.close();
  65.             clientSocket.close();
  66.         } catch (IOException e) {
  67.             System.err.println("Problem with Communication Server");
  68.             System.exit(1);
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement