0

在这个程序中,我的服务器从客户端获取一个命令,后跟 1 或 2 个操作数,并返回操作结果。

我在扫描客户端输入行和在 switch 语句中执行实际操作时遇到问题,如果有人能指出我正确的方向,我将不胜感激。

这是代码:

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

// Takes in a mathematical operation and the operands from a client and returns the result
// Valid operations are add, sub, multiply, power, divide, remainder, square 
public class MathServer 
{ 
    public static void main(String [] args) throws IOException 
    { 
        ServerSocket yourSock = new ServerSocket(50000); //put server online
        while(true)  
        { 
            System.out.println("Waiting to accept connection");
            Socket clientSock = yourSock.accept();  //open server to connections
            System.out.println("Connection accepted");
            process(clientSock);                    //process accepted connection
            System.out.println("Connection closed");
        } 
    }  

    //BufferedReader(Reader r)
    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);       

        String input = br.readLine(); //get user input from client         

        while(input != null && !input.equals("bye")) //check for input, if bye exit connection
        {              
            int answer = operate(input); //perform desired operation on user input
            pw.println(answer);          //print out result
            input = br.readLine();       //get next line of input 
        } 
        sock.close(); 
    }  

    //Talk to the client          
    static int operate(String s) 
    { 
        System.out.println(s); //check if same as client input

        Scanner myScanner = new Scanner(s); 
        String opType = myScanner.next();   //gets desired operation

        System.out.println(opType); //checks for correct operation 

        switch (opType) { 
            case "add": 
                return (myScanner.nextInt() + myScanner.nextInt());
            case "sub":
                return (myScanner.nextInt() - myScanner.nextInt());
            case "multiply":
                return (myScanner.nextInt() * myScanner.nextInt());
            case "power":
                return (int) Math.pow(myScanner.nextInt(), myScanner.nextInt());
            case "divide":
                return myScanner.nextInt() / myScanner.nextInt();
            case "remainder":
                return myScanner.nextInt() % myScanner.nextInt();
            case "square":
                return (int) Math.pow(myScanner.nextInt(), 2);
            default:
                return (int) Math.pow(myScanner.nextInt(), 3);
        }        
    }
} 
4

2 回答 2

1

当您BufferedReade.readLine()在服务器中阅读时,请确保从客户端发送换行符(常见错误)。此外,您可能需要OutputStream从客户端刷新。由于您Scanner读取变量的方式,您需要从客户的单行发送值,例如

add 100 200
于 2013-01-30T01:08:36.560 回答
0

switch(opType)不适用于字符串。

检查类似的东西

if(opType.equals("add")){ //do Add }
else if(opType.equals("sub")){ //do subtraction } 

等等

于 2013-01-30T00:59:02.700 回答