2075 12

CSC419 ยท 2075 ยท 5 marks

What is a socket? Write client and server programs in which a server program accepts a radius of a circle from the client program. Computes area, sends the computed area to the client program, and displays it by client program.

What is a socket? Write client and server programs in which a server program accepts a radius of a circle from the client program. Computes area, sends the computed area to the client program, and displays it by client program. [5]

Advanced Java Programming

Quick answer

A socket is an endpoint for communication between two machines over a network. It provides a mechanism for two programs (client and server) to communicate with each other using standard input/output streams. In Java, the Socket class (client side) and ServerSocket class (server side) from the java.net package are used to establish TCP-based socket communication.

Socket Programming: Circle Area Computation

What is a Socket?


Steps for Writing Server Program:

  1. Create an object of ServerSocket class
  2. Accept the connection from the client
  3. Get input and output streams from the client
  4. Read/Write data to the socket
  5. Close streams and socket

Steps for Writing Client Program:

  1. Open a socket
  2. Open input and output streams to the socket
  3. Read from and write to the socket streams
  4. Close the socket

Server Program

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

public class CircleServer {
    public static void main(String args[]) throws IOException {

        // Step 1: Create ServerSocket on port 1254
        ServerSocket ss = new ServerSocket(1254);
        System.out.println("Server is waiting for client...");

        // Step 2: Accept connection from client
        Socket cs = ss.accept();
        System.out.println("Client connected.");

        // Step 3: Get input and output streams
        Scanner ins = new Scanner(cs.getInputStream());
        PrintWriter outs = new PrintWriter(cs.getOutputStream(), true);

        // Step 4: Read radius sent by client
        double radius = ins.nextDouble();
        System.out.println("Radius received from client: " + radius);

        // Step 5: Compute area of circle
        double area = Math.PI * radius * radius;
        System.out.println("Computed Area: " + area);

        // Step 6: Send computed area back to client
        outs.println(area);

        // Step 7: Close streams and socket
        ins.close();
        outs.close();
        cs.close();
        ss.close();
    }
}

Client Program

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

public class CircleClient {
    public static void main(String args[]) throws IOException {

        // Step 1: Open a socket and connect to server at localhost, port 1254
        Socket cs = new Socket("localhost", 1254);

        // Step 2: Open input and output streams
        Scanner ins = new Scanner(cs.getInputStream());
        PrintWriter outs = new PrintWriter(cs.getOutputStream(), true);

        // Step 3: Read radius from user
        Scanner userInput = new Scanner(System.in);
        System.out.print("Enter radius of circle: ");
        double radius = userInput.nextDouble();

        // Step 4: Send radius to server
        outs.println(radius);

        // Step 5: Receive computed area from server
        double area = ins.nextDouble();

        // Step 6: Display the area
        System.out.println("Area of circle received from Server: " + area);

        // Step 7: Close streams and socket
        ins.close();
        outs.close();
        cs.close();
    }
}

Sample Output

-- Server Side --
Server is waiting for client...
Client connected.
Radius received from client: 7.0
Computed Area: 153.93804002589985

-- Client Side --
Enter radius of circle: 7
Area of circle received from Server: 153.93804002589985

Summary of Communication Flow

Client                          Server
  |                               |
  |--- sends radius (7.0) ------> |
  |                               | computes area = ฯ€ * rยฒ
  |<-- receives area (153.93) --- |
  |                               |
displays area                  displays area

Note: Both programs use port 1254 for communication. The server must be started before the client. The formula used is Area = ฯ€ ร— rยฒ computed using Math.PI in Java.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.