Network Server Program

Verified

Added on  2019/09/16

|3
|469
|309
Practical Assignment
AI Summary
This assignment involves creating a Java network server program that can handle basic arithmetic operations (addition, subtraction, multiplication, and division). The server accepts requests from clients, processes them, and sends back the results. The server uses sockets for communication and handles potential errors such as invalid input or division by zero. The code is well-structured and uses enums for better readability and maintainability. The solution demonstrates a fundamental understanding of network programming concepts in Java. The provided code is a complete, functional solution to the assignment.
Document Page
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Support add, subtraction, multiplication, division
* @author hluu
*
*/
public class MyServer {
enum OPERATOR { ADD, SUB, MULT, DIV };
public static final int PORT_NO = 12345;
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket serverSocket = new ServerSocket(PORT_NO);
System.out.println("... server is accepting request");
while (true) {
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
Socket socket = serverSocket.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = reader.readLine();
if (line.trim().startsWith("EXIT")) {
System.out.println("... server shutting down ...");
socket.close();
break;
} else {
System.out.println("Got Request " + line);
processRequest(socket, line);
}
}
System.out.println("... closing server socket ...");
serverSocket.close();
}
private static void processRequest(Socket socket, String line) throws IOException, InterruptedException {
PrintWriter pw = new PrintWriter(socket.getOutputStream());
String[] tokens = line.split(" ");
String operator = tokens[0].trim();
try {
Double operand1 = Double.valueOf(tokens[1].trim());
Double operand2 = Double.valueOf(tokens[2].trim());
System.out.println(operand1);System.out.println(operand2);
Document Page
double result = 0;
OPERATOR op = OPERATOR.valueOf(operator.toUpperCase());
switch (op) {
case ADD:
result = operand1 + operand2;
break;
case SUB:
result = operand1 - operand2;
break;
case MULT:
result = operand1 * operand2;
break;
case DIV:
result = operand1 / operand2;
break;
default:
pw.println("invalid operand: " + line);
pw.flush();
socket.close();
return;
}
System.out.println("Response: " + result );
pw.println(result);
} catch (NumberFormatException e) {
pw.println("invalid operand: " + line);
}
pw.flush();
socket.close();
}
}
chevron_up_icon
1 out of 3
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]