Advertisement
dodiindika

TCPThreadServer

Jan 3rd, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. public class TCPThreadServer {
  6.    
  7.     private static ServerSocket serverSocket;
  8.     private static final int PORT = 9393;
  9.     public static void main(String[] args)
  10.             throws IOException    
  11.     {
  12.         System.out.println("Opening port...\n");
  13.         try      
  14.         {          
  15.             serverSocket = new ServerSocket(PORT);
  16.         }
  17.         catch (IOException ioEx)      
  18.         {          
  19.             System.out.println("\nUnable to set up port!");
  20.             System.exit(1);
  21.         }
  22.         do      
  23.         {
  24.             //Wait for client...
  25.             Socket client = serverSocket.accept();
  26.            
  27.             System.out.println("\nNew client accepted.\n");
  28.          
  29.             //Create a thread to handle communication with
  30.             //this client and pass the constructor for this
  31.             //thread a reference to the relevant socket...
  32.             ClientHandler handler =
  33.                     new ClientHandler(client);
  34.             handler.start();//As usual, method calls run.
  35.         }while (true);    
  36.     }
  37. }
  38.  
  39. class ClientHandler extends Thread
  40. {
  41.     private Socket client;
  42.     private Scanner input;
  43.     private PrintWriter output;
  44.    
  45.    public ClientHandler(Socket socket)
  46.    {
  47.        //Set up reference to associated socket...
  48.        client = socket;
  49.       try
  50.       {
  51.           input = new Scanner(client.getInputStream());
  52.           output = new PrintWriter(
  53.                   client.getOutputStream(),true);
  54.       }
  55.       catch(IOException ioEx)
  56.               {
  57.                   ioEx.printStackTrace();
  58.               }
  59.    }
  60.    public void run()
  61.    {
  62.        String message;
  63.  
  64.       do
  65.       {
  66.          //Accept message from client on
  67.           //the socket's input stream...
  68.           message = input.nextLine();
  69.          
  70.          //Echo message back to client on
  71.          //the socket's output stream...
  72.          output.println("ECHO: " + message);
  73.          
  74.          //Repeat above until 'QUIT' sent by client...
  75.       }while (!message.equals("QUIT"));
  76.      
  77.       try
  78.       {
  79.           if (client!=null)
  80.           {
  81.               System.out.println(
  82.                       "Closing down connection...");
  83.               client.close();
  84.           }
  85.       }
  86.       catch(IOException ioEx)
  87.       {
  88.           System.out.println("Unable to disconnect!");
  89.       }
  90.    }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement