Answer to Question #275424 in Java | JSP | JSF for hatipo

Question #275424

Create your own NetworkServer and NetworkClient applications as following.we will name them as MyComputerServer and MyComputingClient .The Client send two numbers and the server send back addition of the two numbers


1
Expert's answer
2021-12-04T20:48:09-0500
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class MyComputerClient {
    public static void main(String[] args) {
        try (Socket socket = new Socket("localhost", 49000)) {
            PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer.println(2 + " " + 4);
            System.out.println(reader.readLine());
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class MyComputerServer {
    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(49000)) {
            Socket socket = serverSocket.accept();
            System.out.println("Connection accepted.");
            PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String[] data = reader.readLine().split(" ");
            writer.println(Integer.parseInt(data[0]) + Integer.parseInt(data[1]));
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS