0% found this document useful (0 votes)
2 views

Solution Mini Project

The document contains Java code for a mini project involving UDP and TCP servers and clients that perform basic arithmetic operations. The UDP server receives expressions from the client, calculates the result, and sends it back, while the TCP server handles multiple clients using threads. Both client types allow users to input math expressions or exit the program, with error handling for invalid inputs and operations.

Uploaded by

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

Solution Mini Project

The document contains Java code for a mini project involving UDP and TCP servers and clients that perform basic arithmetic operations. The UDP server receives expressions from the client, calculates the result, and sends it back, while the TCP server handles multiple clients using threads. Both client types allow users to input math expressions or exit the program, with error handling for invalid inputs and operations.

Uploaded by

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

Mini Project

Name: S. No:
Academic No: Section:
Subject: 371CCS Marks: 5

Solution

1) UDP server code

package server;

import java.net.*;

public class Server {

public static void main(String[] args) {

try {

DatagramSocket socket = new DatagramSocket(9876);

System.out.println("UDP Server started...");

while (true) {

byte[] receiveData = new byte[1024];

DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

socket.receive(receivePacket);

String expression = new String(receivePacket.getData(), 0, receivePacket.getLength());

if (expression.equals("exit")) {

break;

String result = calculate(expression);

InetAddress clientAddress = receivePacket.getAddress();

int clientPort = receivePacket.getPort();

byte[] sendData = result.getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientAddress, clientPort);

socket.send(sendPacket);

1
System.out.println("UDP Server stopped.");

} catch (Exception e) {

e.printStackTrace();

private static String calculate(String expression) {

String[] tokens = expression.split(" ");

if (tokens.length != 3) {

return "Invalid expression";

double operand1 = Double.parseDouble(tokens[0]);

char operator = tokens[1].charAt(0);

double operand2 = Double.parseDouble(tokens[2]);

double result = 0;

switch (operator) {

case '+':

result = operand1 + operand2;

break;

case '-':

result = operand1 - operand2;

break;

case '*':

result = operand1 * operand2;

break;

case '/':

if (operand2 != 0)

result = operand1 / operand2;

2
else

return "Division by zero error";

break;

default:

return "Invalid operator";

return String.valueOf(result);

2) UDP client code

package client;

import java.net.*;

import java.util.Scanner;

public class Client {

public static void main(String[] args) {

try {

DatagramSocket socket = new DatagramSocket();

InetAddress serverAddress = InetAddress.getByName("localhost");

Scanner scanner = new Scanner(System.in);

3
while (true) {

System.out.println("Choose an option:");

System.out.println("1. Terminate the program");

System.out.println("2. Continue to input math expressions");

int option = scanner.nextInt();

scanner.nextLine();

if (option == 1) {

byte[] sendData = "exit".getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, 9876);

socket.send(sendPacket);

break;

} else if (option == 2) {

System.out.println("Enter math expression (e.g., 2 + 3): ");

String expression = scanner.nextLine();

byte[] sendData = expression.getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, 9876);

socket.send(sendPacket);

byte[] receiveData = new byte[1024];

DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

socket.receive(receivePacket);

String result = new String(receivePacket.getData(), 0, receivePacket.getLength());

System.out.println("Result: " + result);

} else {

System.out.println("Invalid option. Please choose again.");

socket.close();

4
} catch (Exception e) {

e.printStackTrace();

Output For Four Operations +,-,/,*

3) TCP server code,

package server;

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

public class Server {

public static void main(String[] args) {

try {

ServerSocket serverSocket = new ServerSocket(9876);

System.out.println("TCP Server started...");

while (true) {

Socket clientSocket = serverSocket.accept();

System.out.println("New client connected: " + clientSocket);

new Thread(new ClientHandler(clientSocket)).start();

} catch (Exception e) {

e.printStackTrace();

class ClientHandler implements Runnable {

private Socket clientSocket;

public ClientHandler(Socket clientSocket) {

this.clientSocket = clientSocket;

@Override

public void run() {

try {

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

6
String expression;

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

if (expression.equals("exit")) {

break;

String result = calculate(expression);

out.println(result);

clientSocket.close();

} catch (Exception e) {

e.printStackTrace();

private static String calculate(String expression) {

String[] tokens = expression.split(" ");

if (tokens.length != 3) {

return "Invalid expression";

double operand1 = Double.parseDouble(tokens[0]);

char operator = tokens[1].charAt(0);

double operand2 = Double.parseDouble(tokens[2]);

double result = 0;

switch (operator) {

case '+':

result = operand1 + operand2;

break;

7
case '-':

result = operand1 - operand2;

break;

case '*':

result = operand1 * operand2;

break;

case '/':

if (operand2 != 0)

result = operand1 / operand2;

else

return "Division by zero error";

break;

default:

return "Invalid operator";

return String.valueOf(result);

8
4) TCP client code.

package client;

import java.io.*;

import java.net.*;

import java.util.Scanner;

public class Client {

public static void main(String[] args) {

try {

Socket socket = new Socket("localhost", 9876);

System.out.println("Connected to TCP Server...");

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("Choose an option:");

System.out.println("1. Terminate the program");

System.out.println("2. Continue to input math expressions");

int option = scanner.nextInt();

scanner.nextLine(); // Consume newline

if (option == 1) {

out.println("exit");

break;

} else if (option == 2) {

System.out.println("Enter math expression (e.g., 2 + 3): ");

String expression = scanner.nextLine();

out.println(expression);
9
String result = in.readLine();

System.out.println("Result: " + result);

} else {

System.out.println("Invalid option. Please choose again.");

socket.close();

} catch (Exception e) {

e.printStackTrace();

10

You might also like