0% found this document useful (0 votes)
22 views5 pages

6th CN Programs

The document contains Java implementations for an Echo Server and Client, which handle client connections and echo messages, including special commands for date and random character generation. Additionally, it includes a Salary Server and Client that calculate and return the net salary based on user-provided salary components. Both server-client pairs utilize sockets for communication and handle input/output streams accordingly.

Uploaded by

sai prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

6th CN Programs

The document contains Java implementations for an Echo Server and Client, which handle client connections and echo messages, including special commands for date and random character generation. Additionally, it includes a Salary Server and Client that calculate and return the net salary based on user-provided salary components. Both server-client pairs utilize sockets for communication and handle input/output streams accordingly.

Uploaded by

sai prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

6th program:

EchoServer

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

public class EchoServer {


private static final int DEFAULT_PORT = 12345; // default port
private static ServerSocket serverSocket;

public static void main(String[] args) {


int port = DEFAULT_PORT;

// Get port number from command-line arguments, if any


if (args.length > 0) {
try {
port = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.out.println("Invalid port number, using default: " +
DEFAULT_PORT);
}
}

try {
serverSocket = new ServerSocket(port);
System.out.println("Server is listening on port " + port);

// Continuously accept new clients and spawn a new thread for each
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " +
clientSocket.getInetAddress());

// Handle each client in a new thread


new ClientHandler(clientSocket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}

// ClientHandler class to handle each client's communication


static class ClientHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;

public ClientHandler(Socket socket) {


this.clientSocket = socket;
}

public void run() {


try {
// Set up input and output streams
in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);

String clientMessage;

while ((clientMessage = in.readLine()) != null) {


if (clientMessage.equalsIgnoreCase("exit")) {
break;
}

// Handle date and time request


if (clientMessage.equalsIgnoreCase("date")) {
out.println("Server date and time: " + new Date());
}
// Handle random character generation
else if (clientMessage.equalsIgnoreCase("generate")) {
out.println("Random character: " + (char) ('A' +
Math.random() * 26));
}
// Echo back the received message
else {
out.println("Echo: " + clientMessage);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

EchoClient

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

public class EchoClient {


private static final String SERVER_HOST = "localhost"; // server host
private static final int DEFAULT_PORT = 12345; // default server port

public static void main(String[] args) {


int port = DEFAULT_PORT;

// Get port number from command-line arguments, if any


if (args.length > 0) {
try {
port = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.out.println("Invalid port number, using default: " +
DEFAULT_PORT);
}
}
try (Socket socket = new Socket(SERVER_HOST, port);
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in));
BufferedReader serverResponse = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {

System.out.println("Connected to server at " + SERVER_HOST + " on port


" + port);
String userMessage;

while (true) {
System.out.print("Enter message (or 'exit' to quit, 'date' for
date/time, 'generate' for random char): ");
userMessage = userInput.readLine();

if (userMessage.equalsIgnoreCase("exit")) {
out.println(userMessage);
break;
}

out.println(userMessage);

// Print server's response


String response = serverResponse.readLine();
System.out.println("Server: " + response);
}

} catch (IOException e) {
e.printStackTrace();
}
}
}

10th program:-
SalaryServer.java

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

public class SalaryServer {


public static void main(String[] args) {
try {
// Create a server socket on port 1234
ServerSocket serverSocket = new ServerSocket(1234);
System.out.println("Server started. Waiting for client...");

while (true) {
// Accept incoming client connections
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " +
clientSocket.getInetAddress());

// Create input and output streams for communication


DataInputStream input = new
DataInputStream(clientSocket.getInputStream());
DataOutputStream output = new
DataOutputStream(clientSocket.getOutputStream());
// Read details from the client
double basic = input.readDouble();
double hra = input.readDouble();
double da = input.readDouble();
double pt = input.readDouble();
double epf = input.readDouble();

// Calculate net salary


double netSalary = basic + hra + da - pt - epf;

// Send the net salary back to the client


output.writeDouble(netSalary);

// Close the streams and the client connection


input.close();
output.close();
clientSocket.close();
}
} catch (IOException e) {
System.out.println("Server error: " + e.getMessage());
}
}
}

SalaryClient.java

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

public class SalaryClient {


public static void main(String[] args) {
try {
// Connect to the server running on localhost and port 1234
Socket socket = new Socket("localhost", 1234);
System.out.println("Connected to the server.");

// Create input and output streams for communication


DataInputStream input = new DataInputStream(socket.getInputStream());
DataOutputStream output = new
DataOutputStream(socket.getOutputStream());

// Use scanner to take input from the user


Scanner scanner = new Scanner(System.in);

// Input details from the user


System.out.print("Enter Basic Salary: ");
double basic = scanner.nextDouble();
System.out.print("Enter HRA: ");
double hra = scanner.nextDouble();
System.out.print("Enter DA: ");
double da = scanner.nextDouble();
System.out.print("Enter PT: ");
double pt = scanner.nextDouble();
System.out.print("Enter EPF: ");
double epf = scanner.nextDouble();
// Send these details to the server
output.writeDouble(basic);
output.writeDouble(hra);
output.writeDouble(da);
output.writeDouble(pt);
output.writeDouble(epf);

// Receive the net salary from the server


double netSalary = input.readDouble();
System.out.println("Net Salary is: " + netSalary);

// Close the streams and socket


input.close();
output.close();
socket.close();
scanner.close();

} catch (IOException e) {
System.out.println("Client error: " + e.getMessage());
}
}
}

You might also like