Sure, here's a basic outline of how you can accomplish this task:
Server-side code:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(12345);
System.out.println("Server started. Waiting for client...");
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
// Read numbers from client
double num1 = Double.parseDouble(in.readLine());
double num2 = Double.parseDouble(in.readLine());
// Perform addition
double result = num1 + num2;
// Send result back to client
out.println("The sum is: " + result);
// Close connection
clientSocket.close();
}
}
}
Client-side code:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
// Connect to server
Socket socket = new Socket("localhost", 12345);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// Input numbers from user
System.out.println("Enter first number:");
double num1 = scanner.nextDouble();
System.out.println("Enter second number:");
double num2 = scanner.nextDouble();
// Send numbers to server
out.println(num1);
out.println(num2);
// Receive result from server
String result = in.readLine();
System.out.println("Result from server: " + result);
// Close connection
socket.close();
scanner.close();
}
}
Make sure to run the server before running the client. This code sets up a simple client-server architecture where the client sends two numbers to the server, the server performs the addition, and sends the result back to the client.
Once you've run the server and the client, you should see the result of the addition displayed on the client's side. If you encounter any difficulties with your Java assignment or need further assistance with programming tasks, don't hesitate to seek guidance from reliable sources. There are various resources available online that can provide
help with Java assignment and programming tasks, offering valuable insights and support to enhance your learning experience.