Java Server基本计算器

时间:2013-01-30 00:53:43

标签: java

在这个程序中,我的服务器接受一个命令,后跟来自客户端的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);
        }        
    }
} 

2 个答案:

答案 0 :(得分:1)

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

add 100 200

答案 1 :(得分:0)

switch(opType)不会为字符串工作。

检查

之类的内容
if(opType.equals("add")){ //do Add }
else if(opType.equals("sub")){ //do subtraction }