Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Please help

  1. #1
    Junior Member
    Join Date
    Apr 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help

    Assignment is next: You need to create an application for adding two numbers. The addition operation itself needs to take place on the server, which accepts the two additions and delivers the result. Therefore, it is necessary that the solution contains two components: server and client.

    Within the client application, the user needs to be able to enter two numbers using the Scanner class. Values ​​need to be placed inside variables and then sent to the server part of the application. Values ​​can be sent together or separately. After accepting the value, the server should add the passed numbers and deliver the total to the client. The obtained total must be displayed on the client at the output.


    I can't manage to code so that the addition is done on the server and the result is sent to the client. Please help me.

  2. #2
    Member Helium c2's Avatar
    Join Date
    Nov 2023
    Location
    Kekaha, Kaua'i
    Posts
    102
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please help

    Actually every computer has the capability of doing math computations. It does matter if it's on a server or client. The idea is passing programs across networks. Like png files. Server to client. Any computer does math computations.

  3. #3
    Junior Member
    Join Date
    Jan 2024
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Post Re: Please help

    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.