2013-03-20 229 views
0

我需要做的基本上是客戶端要求服務器執行加法或乘法的Java客戶端/服務器程序。客戶端發送一個 對象,該對象包含請求的操作(加法或乘法)和服務器必須處理的一系列數字(加法或乘法)。號碼由控制檯發送,由用戶編寫。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(); 

} 

    } 
+1

你的代碼包含錯誤,格式不正確。如果你清理一下它會更容易。 – NilsH 2013-03-20 22:11:23

回答

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(); 
    } 
}