JAVA Client/Server 计算器示例
JAVA Client/Server Calculator Example
我们接到了一项网络任务来实施 client/server 项目。我们得到的一个例子是 Client/Server 计算器程序。我已经在下面发布了 类 的代码。我正在使用 eclipse,在我的 运行 配置设置中,我输入 'ADD,5,6' 作为客户端程序的参数,但输出为空。你们能看出为什么会这样吗?
客户代码:
package networking;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class CalculatorClient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
String operation = args[0];
// Create a new socket object and names it socket
// the constructor requires the name of the computer and the port number to which you want to connect
Socket socket = new Socket("localhost", CalculatorServer.PORT_NO);
// gets the socket's input stream and opens a BufferedReader on it.
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// gets the socket's output stream and opens a PrintWriter on it
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.println(operation);
pw.flush();
if (!"quit".equals(operation.trim())) {
String line = reader.readLine();
reader.close();
System.out.println("Line 36 in Client");
System.out.println("result: " + line);
System.out.println("Line 38 in Client");
}
socket.close();
}
}
服务器代码:
package networking;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Support add, subtraction, multiplication, division
* @author hluu
*
*/
public class CalculatorServer {
enum OPERATOR { ADD, SUB, MULT, DIV };
public static final int PORT_NO = 8888;
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket serverSocket = new ServerSocket(PORT_NO);
System.out.println("... server is accepting request");
while (true) {
Socket socket = serverSocket.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = reader.readLine();
System.out.println("got request: " + line);
System.out.println("Right after getting request");
if (line.trim().startsWith("quit")) {
System.out.println("... server shutting down ...");
socket.close();
break;
} else {
System.out.println("Entering Else");
System.out.println(line);
processRequest(socket, line);
}
}
System.out.println("... closing server socket ...");
serverSocket.close();
}
private static void processRequest(Socket socket, String line) throws IOException, InterruptedException {
PrintWriter pw = new PrintWriter(socket.getOutputStream());
String[] tokens = line.split(" ");
if (tokens.length != 2) {
pw.println("invalid command: " + line);
socket.close();
return;
}
String[] operands = tokens[1].split(",");
if (operands.length != 2) {
pw.println("invalid command: " + line);
socket.close();
return;
}
String operator = tokens[0].trim();
try {
Double operand1 = Double.valueOf(operands[0].trim());
Double operand2 = Double.valueOf(operands[1].trim());
System.out.println(operand1);System.out.println(operand2);
double result = 0;
OPERATOR op = OPERATOR.valueOf(operator.toUpperCase());
switch (op) {
case ADD:
result = operand1 + operand2;
break;
case SUB:
System.out.println("Entering SUB");
result = operand1 - operand2;
break;
case MULT:
result = operand1 * operand2;
break;
case DIV:
result = operand1 / operand2;
break;
default:
pw.println("invalid operand: " + line);
pw.flush();
socket.close();
return;
}
System.out.println("send back result: " + result );
pw.println(result);
} catch (NumberFormatException nfe) {
pw.println("invalid operand: " + line);
}
pw.flush();
socket.close();
}
}
在大多数情况下,调试是查看正在发生的事情的一种方式。
结果表明,您的论点的正确格式是:
ADD 5,6
但是 CalculatorClient
中的 operation
字符串仅抓取 ADD
部分,因为 args
内容实际上是:{"ADD", "5,6"}
。因此,要形成一个有效的请求,您应该以某种方式连接您的输入:
String operation = args[0] + " " + args[1];
我们接到了一项网络任务来实施 client/server 项目。我们得到的一个例子是 Client/Server 计算器程序。我已经在下面发布了 类 的代码。我正在使用 eclipse,在我的 运行 配置设置中,我输入 'ADD,5,6' 作为客户端程序的参数,但输出为空。你们能看出为什么会这样吗?
客户代码:
package networking;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class CalculatorClient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
String operation = args[0];
// Create a new socket object and names it socket
// the constructor requires the name of the computer and the port number to which you want to connect
Socket socket = new Socket("localhost", CalculatorServer.PORT_NO);
// gets the socket's input stream and opens a BufferedReader on it.
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// gets the socket's output stream and opens a PrintWriter on it
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.println(operation);
pw.flush();
if (!"quit".equals(operation.trim())) {
String line = reader.readLine();
reader.close();
System.out.println("Line 36 in Client");
System.out.println("result: " + line);
System.out.println("Line 38 in Client");
}
socket.close();
}
}
服务器代码:
package networking;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Support add, subtraction, multiplication, division
* @author hluu
*
*/
public class CalculatorServer {
enum OPERATOR { ADD, SUB, MULT, DIV };
public static final int PORT_NO = 8888;
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket serverSocket = new ServerSocket(PORT_NO);
System.out.println("... server is accepting request");
while (true) {
Socket socket = serverSocket.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = reader.readLine();
System.out.println("got request: " + line);
System.out.println("Right after getting request");
if (line.trim().startsWith("quit")) {
System.out.println("... server shutting down ...");
socket.close();
break;
} else {
System.out.println("Entering Else");
System.out.println(line);
processRequest(socket, line);
}
}
System.out.println("... closing server socket ...");
serverSocket.close();
}
private static void processRequest(Socket socket, String line) throws IOException, InterruptedException {
PrintWriter pw = new PrintWriter(socket.getOutputStream());
String[] tokens = line.split(" ");
if (tokens.length != 2) {
pw.println("invalid command: " + line);
socket.close();
return;
}
String[] operands = tokens[1].split(",");
if (operands.length != 2) {
pw.println("invalid command: " + line);
socket.close();
return;
}
String operator = tokens[0].trim();
try {
Double operand1 = Double.valueOf(operands[0].trim());
Double operand2 = Double.valueOf(operands[1].trim());
System.out.println(operand1);System.out.println(operand2);
double result = 0;
OPERATOR op = OPERATOR.valueOf(operator.toUpperCase());
switch (op) {
case ADD:
result = operand1 + operand2;
break;
case SUB:
System.out.println("Entering SUB");
result = operand1 - operand2;
break;
case MULT:
result = operand1 * operand2;
break;
case DIV:
result = operand1 / operand2;
break;
default:
pw.println("invalid operand: " + line);
pw.flush();
socket.close();
return;
}
System.out.println("send back result: " + result );
pw.println(result);
} catch (NumberFormatException nfe) {
pw.println("invalid operand: " + line);
}
pw.flush();
socket.close();
}
}
在大多数情况下,调试是查看正在发生的事情的一种方式。 结果表明,您的论点的正确格式是:
ADD 5,6
但是 CalculatorClient
中的 operation
字符串仅抓取 ADD
部分,因为 args
内容实际上是:{"ADD", "5,6"}
。因此,要形成一个有效的请求,您应该以某种方式连接您的输入:
String operation = args[0] + " " + args[1];