Java-TCP socket programming


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

public class Client1 {
  public static void main(String[] args) throws Exception {
    String hostAddr = "127.0.0.1"; // Loopback Address
    int hostPort = 8000;

    System.out.println("Client requesting for connection...");
    Socket clientSideSocket = new Socket(hostAddr, hostPort);
    System.out.println("Connection established.");

    PrintWriter serverWriter = new PrintWriter(
                clientSideSocket.getOutputStream(), true);

    System.out.println("Key in a message for the server:");
    serverWriter.println(new BufferedReader(
                new InputStreamReader(System.in)).readLine());
    System.out.println("Message sent to server.");
  }
}
---------------------- ----------------------
import java.net.*;
import java.io.*;

public class Server1 {
  public static void main(String[] args) throws Exception {
    int port = 8000;
    ServerSocket serverSocket = new ServerSocket(port);

    System.out.println("Server Started. Waiting for connection...");
    Socket serverSideClientSocket = serverSocket.accept();
    System.out.println("Client connection request received.");
    System.out.println("Sockets bound and ready for communication.");

    BufferedReader clientReader = new BufferedReader(new InputStreamReader(
                serverSideClientSocket.getInputStream()));
    
    System.out.println("Waiting for message from client...");
    String messageFromClient = clientReader.readLine();
    System.out.println("Client says: " + messageFromClient);
  }
}

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

public class Client2 {
  public static void main(String args[]) {
    try {
      Socket clientSideSocket = new Socket("localhost", 8000);
      BufferedReader serverReader = new BufferedReader(
                new InputStreamReader(clientSideSocket.getInputStream()));
      PrintWriter serverWriter = new PrintWriter(
                     clientSideSocket.getOutputStream(), true);
                BufferedReader keyboardReader = new BufferedReader(
                     new InputStreamReader(System.in));
     
                double radius = 0;
      while (true) {
        System.out.print("Please enter the Radius of a Circle...");
        radius = Double.parseDouble(keyboardReader.readLine());
        serverWriter.println(radius);
                     if (radius != 0) {
                           double area = Double.parseDouble(
serverReader.readLine());
                           System.out.println("Area received from the Server: " +
                                area);
                     } else {
                           break;
                     }
      }
    } catch(IOException e) {
    }
  }
}

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

public class Server2 {
  public static void main(String[] args) {
    try {
      ServerSocket serverSocket = new ServerSocket(8000);
      Socket serverSideClientSocket = serverSocket.accept();

      BufferedReader clientReader = new BufferedReader(
                     new InputStreamReader(
                     serverSideClientSocket.getInputStream()));
      PrintWriter clientWriter = new PrintWriter(
                     serverSideClientSocket.getOutputStream(), true);
      while (true) {
        double radius = Double.parseDouble(clientReader.readLine());
                     if (radius != 0) {
                           System.out.println("Radius Received from Client: " +
                                radius);
                           double area = radius * radius * Math.PI;
                           clientWriter.println(area);
                     } else {
                           break;
                     }
      }
    } catch(IOException e) {
      //System.out.println(e);
    }
  }
}

Post a Comment

0 Comments