0

基本的に私がする必要があるのは、クライアントがサーバーに加算または乗算を実行するように要求するJavaクライアント/サーバープログラムです。クライアントは、要求された操作(加算または乗算)とサーバーが処理する必要のある一連の数値(加算または乗算)を含むオブジェクトをサーバーに送信します。番号は、ユーザーが書き込んだコンソールから送信されます。

サーバーはオブジェクトを受け取り、計算を行い、答えを返します。これは私がこれまでに持っているコードです。add関数をオブジェクトに配置し、サーバーに送信し、番号を送信して、サーバーがそれらに作用できるようにする方法がわかりません。

         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 に答える