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
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();
}
}
}
Comments
Leave a comment