Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Just need help creating a UDP calculator with comunication from a client to a se

ID: 3716308 • Letter: J

Question

Just need help creating a UDP calculator with comunication from a client to a server . TCP part already completed

Behavior on the client side 1. The client program should be able to run in a forever while loop, so that the user can send as many calculation requests as he/she wants. The client can input a math expression that it wants to calculate, such as "50 + 100", "50 * 100" etc. 2. 3. In each iteration of the while loop, the client program should provide two choices for the user: 1.) terminate the program, indicating that the user no longer needs to do any calculation this time. 2.) continue to input math expressions. This is like providing a menu for the user. You don't need to design Graphic User Interface (GUI). The client program can just print out some instructions for the user, and determine what the user wants according to the user's input. . The math expression is input as a string. You need to send the calculation request to the Simple Calculator Server using UDP socket and TCP socket. In this aspect, the message sending process is very similar to the example application we discussed in class. 5. If the user explicitly indicates that he/she wants to exit the program, the program should properly close any socket previously used for communications. Behavior on the server side: 1. 2. The server should have a while loop that can continuously accept calculation requests. Server should understand at least the four operators, i.e., +,-. *, and /. when they appear alone. Expressions are received as strings. Example expressions: "40 50, "20 - 100", "10 20, "100/ 1000. It should be flexible enough such that if the expressions are reasonable, they should be considered valid and accepted. For example, these expressions: "www40-50 "-40 + 50 , and "40+50, are all valid and should be accepted. That is, adding or removing arbitrary spaces anywhere in the expression shouldn't be a problem. 3. The result of a calculation request will be sent back to the corresponding requester as a string, which represents a number The server should be able to serve multiple clients, at least one client by one client. 4.

Explanation / Answer

Client.Java

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.net.SocketTimeoutException;

public class Client {

public static void main(String[] args){

//server assumed to be @ local host else provide host name, and can use any port 0-65535

//but first 1024 (0-1023) are well known ports and used for special purposes

//Hence we can use any port no from 1024 to 65535 as server port

//but make sure both client and server listens to same port.

DatagramSocket clientSocket = null;

try {

clientSocket = new DatagramSocket();

int cont=1;//value indicates wants to continue

while(cont==1){

String expression = displayMainMenuAndGetExpression();

//if user does not want to quit read the expression, send to server

byte[] expressionAsBytes = expression.getBytes();

DatagramPacket clientPacketSend =new DatagramPacket(expressionAsBytes, expressionAsBytes.length, InetAddress.getLocalHost(), 2000);//packet to send

clientSocket.send(clientPacketSend);

//clientSocket.setSoTimeout(1000);;

if(!expression.trim().equalsIgnoreCase("exit")){

System.out.println("sent expression to server");

byte[] resultAsBytes = new byte[65535];

DatagramPacket clientPacketRecv = new DatagramPacket(resultAsBytes, resultAsBytes.length);//packet to receive

clientSocket.receive(clientPacketRecv);

String result = new String(clientPacketRecv.getData());

System.out.println(expression+" = "+ result);

}else{

System.out.println("Exiting");

return;

}

}

} catch(SocketTimeoutException e){

System.out.println("Failed to get response from server: either the server is timed out or an invalid expression");

}catch (IOException e) {

System.out.println("Failed to establish with a server @ localhost:2000 with error:"+e.getMessage());

}

}

//function to read user input 1->Expression 2->Quit

private static String displayMainMenuAndGetExpression() throws IOException {

System.out.println("Select One Option");

System.out.println("1.Enter Expression");

System.out.println("2.Quit");

BufferedReader dis = new BufferedReader(new InputStreamReader(System.in));

String option = dis.readLine();

String expression;

if(option.equalsIgnoreCase("2")){

expression= "Exit";

}else{

System.out.println("Enter the expression");

expression = dis.readLine();

}

return expression;

}

}

Server.java

public class Server {

public static int port = 2000;

private static String validOperators = "+-/*";

public static void main(String[] args) {

DatagramSocket serverSocket = null;

try {

serverSocket = new DatagramSocket(port);

serverSocket.setSoTimeout(65535);

int cont = 1;//will set to 0 when user send to exit

while(cont==1){

System.out.println("Waiting for client @ "+serverSocket.getInetAddress());

byte[]dataBuff = new byte[65535];

DatagramPacket serverPacketRecv = new DatagramPacket(dataBuff, dataBuff.length);//packet to receive

serverSocket.receive(serverPacketRecv);

String expression = new String(serverPacketRecv.getData());

if(expression.trim().equalsIgnoreCase("exit")){//exit condition

cont=0;// exit condition for server

break;

}

String operand1="",operand2="";

String operator="";

int operandsFoundCount = 0;

for(int i=0;i<expression.length();i++){

String ch = expression.substring(i, i+1);//taking each character

if(operandsFoundCount==0){//condition for first operand

if(validOperators.indexOf(ch)==-1){//condition that an operand found

operand1+=ch;

}else{

operandsFoundCount++;//starting next character as second operand

operator = ch;//operator found is stored

}

}else{

operand2+=ch;

}

}

operand1 = operand1.trim();

operand2 = operand2.trim();

String result = "";

try{

switch(operator){

case "+":

result = (Double.parseDouble(operand1)+Double.parseDouble(operand2))+"";

break;

case "-":

result = (Double.parseDouble(operand1)-Double.parseDouble(operand2))+"";

break;

case "*":

result = (Double.parseDouble(operand1)*Double.parseDouble(operand2))+"";

break;

case "/":

if(Double.parseDouble(operand2)!=0)//division by zero condition

result = (Double.parseDouble(operand1)/Double.parseDouble(operand2))+"";

else{

result = "Division by zero error!!!";

}

break;

}

}catch(Exception e){

System.out.println("Exception occurred at server side and the error is :");

result = "Invalid expression!!!";

}

dataBuff = result.getBytes();

DatagramPacket serverPacketSend = new DatagramPacket(dataBuff, dataBuff.length,InetAddress.getLocalHost(),serverPacketRecv.getPort());//reply to the local host @ received port

serverSocket.send(serverPacketSend);

}

} catch (IOException e) {

System.out.println("Failed to open server socket @ "+port+" and the error = "+e.getMessage());

}

}

}

OUTPUT

Select One Option

1.Enter Expression

2.Quit

1

Enter the expression

5+3

sent expression to server

5+3 = 8.0

Select One Option

1.Enter Expression

2.Quit

1

Enter the expression

18/3

sent expression to server

18/3 = 6.0

Select One Option

1.Enter Expression

2.Quit

1

Enter the expression

19/3

sent expression to server

19/3 = 6.333333333333333

Select One Option

1.Enter Expression

2.Quit

1

Enter the expression

10/0

sent expression to server

10/0 = Division by zero error!!!

Select One Option

1.Enter Expression

2.Quit

1

Enter the expression

dvav+acvv

sent expression to server

dvav+acvv = Invalid expression!!!

Select One Option

1.Enter Expression

2.Quit

2

Exiting