0

我需要做的基本上是一个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();

}

   }
4

1 回答 1

0

客户

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));
        String userInput = stdIn.readLine();
        if (userInput.equals("FINE")){//    All calculations are finished
            request.println("FINE");
        }else{
            StringBuffer sbUserInput = new StringBuffer();
            while(!userInput.equals("SEND")){   //  Get input from user until SEND command received and prepare a string which you have to process in server
                sbUserInput.append(userInput).append(" ");//Add the user input along with a space which eases our server task.
                userInput = stdIn.readLine();
            }
            request.println(sbUserInput.toString());    //  Send it to server
        }
        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"){
            //  Read each string separated by space in the line and check if it is a number or + Or *
            String[] lineContents = line.split(" ");
            int result = 0;
            for(String lineContent : lineContents){
                //  Check if it is + OR *
                //  Take proper action and update "result"
            }
            //  Send the response back to the client
            response.println("Hai detto: " +line);
        }
        request.close();
        response.close();
        communication.close();
        listener.close();
    }
}
于 2013-03-20T22:22:34.643 回答