0

我是 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();

  }


}
4

3 回答 3

0

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

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

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

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

于 2013-01-26T22:16:38.773 回答
0

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

    Scanner sc = new Scanner(System.in);

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

Scanner sc = new Scanner(in);

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

于 2013-01-26T22:20:14.653 回答
0
  • 您需要安排在客户端和服务器之间发送和接收数据的顺序。
  • 正如@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();
于 2013-01-26T22:35:42.807 回答