我需要做的基本上是一个Java客户端/服务器程序,客户端要求服务器执行加法或乘法。客户端向服务器发送一个对象,该对象包含请求的操作(加法或乘法)和服务器必须处理的一系列数字(加法或乘法)。数字是从控制台发送的,由用户编写。
服务器接收对象,进行计算并返回答案。这是我到目前为止的代码。我不知道如何将 add 函数放入对象,将其发送到服务器,发送 te 号码,以便服务器可以对它们进行操作。
package esercizio3;
import java.io.*;
import java.net.*;
public class Client{
public static void main(String[] args) throws Exception {
Socket communication = new Socket("localhost",8888);
BufferedReader response = new BufferedReader(
new InputStreamReader(communication.getInputStream()));
PrintWriter request = new PrintWriter(communication.getOutputStream());
BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in));
request.println(stdIn.readLine());
String line = response.readLine();
System.out.println(line);
response.close();
request.close();
stdIn.close();
communication.close();
}
}
package esercizio3;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket listener = new ServerSocket(8888);
Socket communication = listener.accept();
BufferedReader request = new BufferedReader(
new InputStreamReader(communication.getInputStream()));
PrintWriter response = new PrintWriter(communication.getOutputStream());
String line;
while((line = request.readLine()) != "FINE"){
a = request.
response.println("Hai detto: " +line);
}
request.close();
response.close();
communication.close();
listener.close();
}
}