Java服务器基本计算器
在这个程序中,我的服务器从客户端接收一个后跟1或2个操作数的命令,并返回操作结果 我在扫描客户端输入行和执行switch语句中的实际操作时遇到问题,如果有人能为我指出正确的方向,我将不胜感激 代码如下:Java服务器基本计算器,java,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 oper
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);
}
}
}
开关(opType)
对字符串不起作用
检查一下,比如
if(opType.equals("add")){ //do Add }
else if(opType.equals("sub")){ //do subtraction }
等等。当您在服务器中使用
BufferedReade.readLine()
阅读时,请确保从客户端发送换行符(常见错误)。此外,您可能需要从客户端刷新OutputStream
。由于扫描仪
读取变量的方式,您需要在客户机的单行上发送值,例如
add 100 200
您确定客户端正在发送新行终止符作为there命令的一部分吗
br.readLine
将等待到达新行终止符(a\n
或\r
)否客户端没有发送新行终止符,这是应该包含在客户端中的内容还是服务器通常会覆盖类似的内容?否,客户端需要发送一个行结束符,或者服务器需要能够读取传入输入流的每个字符。如果要使用br.readLine
,客户端必须发送一个行结束符作为命令的一部分(就个人而言,这是更简单的方法)。在向服务器写入文本时,只需添加一个\n
作为要发送的最后一个字符。程序员提示,发送到服务器的确切数据取决于您(假设您也在编写服务器代码)。您只需确保服务器读取的数据与客户端希望发送的数据相同(反之亦然)。我完成了客户端程序,并在输入中添加了“\n”,它返回了正确的答案。谢谢你的帮助,我会在找出下一个bug之后马上回来。在Java 7中。好的,我肯定应该升级到Java 7,我忘记了。不过,我现在使用的语句类型可以工作吗?@cjw with Java 6该程序甚至不会编译,因此它编译、运行和行为不当的事实似乎表明您已经升级到Java 7:-)哈哈,是的,它是Java 7(facepalm),我想我的问题是,当我在switch语句中返回时,它只是将整数替换为opType,还是将整数放入opType,然后从方法中返回?我正在使用putty测试我的服务器,你知道它是否解释了换行符吗?否则,我可以完成我的客户端并使用它,而不是可能需要在Putty中配置CR字符,Windows telnet客户端在这方面应该可以。顺便说一句,我几天前回答了。我在做主题之前搜索过,但没有找到,从现在开始我可以参考它。谢谢。谢谢你的帮助,最初的问题是缺少换行符