【问题标题】:Math Server and Client Java not working数学服务器和客户端 Java 不工作
【发布时间】:2013-01-27 06:49:54
【问题描述】:

我是 Java 计算机网络的新手,一开始并不擅长编程,但是,我能够构建这个应该只接受一行的服务器和客户端。一个函数(加、减、除)和两个整数,然后计算并打印答案。我可以将两者连接起来,但是当我在其中键入行时,它什么也不做。任何见解将不胜感激。谢谢!

服务器

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.lang.Math;


public class MathServer
{


  public static void main(String[] args) throws IOException 
  {

     ServerSocket yourSock = new ServerSocket(50000);
     System.out.println("Please Wait....");

     while(true)
     {
        Socket clientSock = yourSock.accept();
        System.out.println("Connected!");
        process(clientSock);


     }


  }

  static void process(Socket sock) throws IOException
  {
     InputStream in = sock.getInputStream();
     BufferedReader br = new BufferedReader(new InputStreamReader(in));

     OutputStream out = sock.getOutputStream();
     PrintWriter pw = new PrintWriter(out,true);

  //Talk to the client
     String input = br.readLine();

     while(input != null && !input.equals("disconnect"))
     {  

        Scanner sc = new Scanner(System.in);
        String func = sc.nextLine();
        double num1 = sc.nextInt();
        double num2 = sc.nextInt();

        double answer;          

        switch (func)
        {
           case "add":  answer = num1 + num2;
              System.out.println(answer);
              break;

           case "sub":  answer = num1 - num2;
              pw.println(answer); 
              break;

           case "multiply":  answer = num1 * num2;
              pw.println(answer);        
              break;

           case "power":  answer = Math.pow(num1, num2);
              pw.println(answer);
              break;

           case "divide":  answer = num1 / num2;
              pw.println(answer);         
              break;

           case "remainder": answer = num1 % num2;
              pw.println(answer);       
              break;

           case "square": answer = Math.sqrt(num1) ;
              pw.println(answer);       
              break;

           case "cube": answer= Math.cbrt(num1);
              pw.println(answer);        
              break;



        }

        sock.close();
     }

  }
}

客户

import java.io.*;
import java.net.Socket;
import java.util.Scanner;


public class Client
{


  public static void main(String[] args) throws Exception
  {
     Socket Sock = new Socket("localhost", 50000);
     BufferedReader br = new BufferedReader(new InputStreamReader(Sock.getInputStream()));
     PrintWriter pw = new PrintWriter(Sock.getOutputStream(), true);


     Scanner sc = new Scanner(System.in);
     String input;


     while(true)
     {
     //System.out.println("Please enter your function and numbers:");
        input = sc.nextLine();

        pw.println(input);

        if(input.equals("disconnect"))
        {
           System.out.println("You have been disconnected");
           break;

        }


        System.out.println(br.readLine());
     }

     Sock.close();

  }


}

【问题讨论】:

    标签: java networking


    【解决方案1】:
    • 您需要安排在客户端和服务器之间发送和接收数据的顺序。
    • 正如@Jacob 所述,您正在从System.in 读取而不是您的输入数据,您可以从输入String 读取。
    • readLine 语句放在您的while 语句中以继续从客户端读取。

    这看起来像:

    String input;
    while ((input = br.readLine())!= null && !input.equals("disconnect")) {
       Scanner sc = new Scanner(input);
       String func = sc.next();
       double num1 = sc.nextDouble();
       double num2 = sc.nextDouble();
       ...
    }
    

    接下来,将您的 sock.close() 语句移到您的 while 循环之外

    现在,来自客户端的输入命令将显示在一行中,例如

    add 12 15
    

    同时从您的客户端刷新数据:

    pw.flush();
    

    【讨论】:

      【解决方案2】:

      您正在混合从 System.in 读取的扫描仪和代码中的网络连接。

      例如,为什么要从命令行读取服务器中的输入?

      Scanner sc = new Scanner(System.in);
      String func = sc.nextLine();
      double num1 = sc.nextInt();
      double num2 = sc.nextInt();
      

      例如,您可以使用 String 的 split 方法拆分您收到的输入并从中计算

      【讨论】:

        【解决方案3】:

        我认为问题出在服务器代码行

            Scanner sc = new Scanner(System.in);
        

        您应该使用自己的输入流:

        Scanner sc = new Scanner(in);
        

        我以前没有使用过扫描仪。另一种方法是将内容读取为 String 并根据需要进行转换,例如Integer.parseInt()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-04-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-02
          • 1970-01-01
          相关资源
          最近更新 更多