SHARE
TWEET

Untitled

a guest Jun 5th, 2014 9 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.*;
  2. import java.net.*;
  3. public class TCPServer
  4. {
  5.     public static void main(String argv[]) throws Exception
  6.     {
  7.         ServerSocket welcomeSocket = new ServerSocket(3001);
  8.         while(true)
  9.         {
  10.             Socket connectionSocket = welcomeSocket.accept();
  11.             new Thread(new TCPServer(). new Client(connectionSocket)).start();
  12.         }
  13.     }
  14.     public class Client implements Runnable{
  15.         Socket client;
  16.         String clientSentence;
  17.         String capitalizedSentence;
  18.         public Client(Socket s){
  19.             client = s;
  20.         }
  21.         @Override
  22.         public void run(){
  23.             try{
  24.                 BufferedReader inFromClient =
  25.                 new BufferedReader(new InputStreamReader(client.getInputStream()));
  26.                 DataOutputStream outToClient = new DataOutputStream(client.getOutputStream());
  27.                
  28.                 while((clientSentence = inFromClient.readLine())!=null){
  29.                     System.out.println("Received: " + clientSentence);
  30.                     capitalizedSentence = clientSentence.toUpperCase() + '\n';
  31.                     outToClient.writeBytes(capitalizedSentence);
  32.                 }
  33.             }catch(Exception e){e.printStackTrace();}
  34.         }
  35.     }
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top