Posted by : Unknown Friday, 26 June 2015

Client Side Program

import java.io.*;
import java.net.*;
class TCPClient {

  public static void main(String args[]) throws Exception
  {
       String sentence;
       String modifiedSentence;
        BufferedReader inFromUser =
         new BufferedReader(new InputStreamReader(System.in));

      Socket clientSocket = new Socket("127.0.0.1", 6324);

        DataOutputStream outToServer =
          new DataOutputStream(clientSocket.getOutputStream());
   
       BufferedReader inFromServer =
          new BufferedReader(new
            InputStreamReader(clientSocket.getInputStream()));

        sentence = inFromUser.readLine();
        outToServer.writeBytes(sentence + '\n');

        modifiedSentence = inFromServer.readLine();

        System.out.println("FROM SERVER: " + modifiedSentence);

        clientSocket.close();
                 
    }
}

Server Side Program

import java.io.*;
import java.net.*;

class TCPServer {

  public static void main(String args[]) throws Exception
    {
      String clientSentence;
      String capitalizedSentence;

      ServerSocket welcomeSocket = new ServerSocket(6324);
 
      while(true) {
 
            Socket connectionSocket = welcomeSocket.accept();

           BufferedReader inFromClient =
              new BufferedReader(new
              InputStreamReader(connectionSocket.getInputStream()));

          
DataOutputStream  outToClient =
             new DataOutputStream(connectionSocket.getOutputStream());

           clientSentence = inFromClient.readLine();
           System.out.println("FROM client:" + clientSentence); 
           capitalizedSentence = clientSentence.toUpperCase() + '\n';
           outToClient.writeBytes(capitalizedSentence);
        }
    }
}

How to run:

  1. First run server program then next run client program
  2. Give input from client side
  3. Next give input in server side

Client Side Output:

hi
FROM SERVER: HI

Server Side Output:

FROM client:hi
hi da

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Welcome to My Blog

Translate

Popular Post

Total Pageviews

Blog Archive

- Copyright © Learning Java Program - Powered by Blogger -