数学服务器和客户端 Java 不工作 [英] Math Server and Client Java not working
问题描述
我是 Java 计算机网络的新手,一开始并不擅长编程,但是,我能够构建这个应该只接受一行的服务器和客户端.一个函数(加、子、除)和两个整数,然后计算并打印答案.我能够将两者连接起来,但是当我在其中输入行时,它什么也不做.任何见解将不胜感激.谢谢!
I'm new to Java Computer Networking and not so great at programming in the first place, however, I was able to construct this Server and Client that is supposed to accept a just one line. A function (add, sub, divide) and two ints and then compute and print the answer. I'm able to connect the two, but when I type the line in it does nothing. Any insight would be appreciated. Thank you!
服务器
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();
}
}
推荐答案
- 您需要安排在您的客户和您之间发送和接收数据的顺序.服务器.
- 正如@Jacob 所提到的,您正在从
System.in
读取而不是您的输入数据,您可以从输入String
中读取. - 将
readLine
语句放在您的while
语句中,以继续从客户端读取. - You need to arrange the order that you send and receive data between your client & server.
- As mentioned by @Jacob, you are reading from
System.in
instead of your input data, you could read from the inputString
. - Place the
readLine
statement within yourwhile
statemet to continue to read from the client.
这看起来像:
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
循环之外.
Next, move your sock.close()
statement outside of your while
loop.
现在,来自客户端的输入命令将显示在一行中,例如
Now, your input commands from the client will appear on a single line, e.g.
add 12 15
同时从您的客户端刷新数据:
Also flush the data from your client:
pw.flush();
这篇关于数学服务器和客户端 Java 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!