【发布时间】:2020-02-17 16:44:33
【问题描述】:
所以我正在尝试制作一个 java 客户端服务器应用程序。在客户端,我有一个菜单供用户选择。然后我希望服务器做的是用适当的数据响应输入的选项。这是我的代码,我哪里出错了?(我也有一个用函数创建的玩家数据库类,但不需要包含来回答我的问题)
- 服务器类
public class Server {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SQLException {
try {
System.out.println("Server started and awaiting connections....");
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
// Start listening for connections on the server socket
Socket connectToClient = serverSocket.accept();
// Create an input stream to get data from the client
DataInputStream isFromClient = new DataInputStream(
connectToClient.getInputStream());
// Create an output stream to send data to the client
DataOutputStream osToClient = new DataOutputStream(
connectToClient.getOutputStream());
InetAddress clientInetAddress = connectToClient.getInetAddress();
System.out.println("Clients host name is " + clientInetAddress.getHostName());
System.out.println("Clients IP address is " + clientInetAddress.getHostAddress());
// Continuously read from the client and process it,
// and send result back to the client
while (true) {
// Read a string from the input stream
int option = isFromClient.readInt();
// Display option on console
System.out.println("Option received from client: " + option);
// Compute option
//int x = (int) option1();
if (option == 1){
List<players> list = PlayersDB.getAllplayers();
//System.out.println("Number of players " + list.size());
int x = list.size();
// Send the result to the client
osToClient.writeInt(x);
osToClient.flush();
// Print the result to the console
System.out.println("Answer to opton is: " + x);
}
}//end while
}//end try
catch (IOException ex) {
System.err.println(ex);
}//end catch
}}
2.客户端类
public class Client {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
// Create a socket to connect to the server
// Socket connectToServer = new Socket("130.254.32.8", 8000);
Socket connectToServer = new Socket("localhost", 8000);
// Create an input stream to receive data from the server
DataInputStream isFromServer = new DataInputStream(
connectToServer.getInputStream());
// Create an output stream to send data to the server
DataOutputStream osToServer
= new DataOutputStream(connectToServer.getOutputStream());
// Continuously send radius and receive area from the server
while (true) {
// Read the radius from the keyboard
System.out.println("Please select an option:");
System.out.println("1 - Number of players:");
System.out.println("2 - Number of English players:");
int option = scanner.nextInt();
// Send the option selected to the server
osToServer.writeInt(option);
osToServer.flush();
// Get answer from the server
int answer = isFromServer.readInt();
// Print answer on the console
System.out.println("Answer to the selected option is " + answer);
}//end while
}//end try
catch (IOException ex) {
System.err.println(ex);
}//end catch
}
}
【问题讨论】:
标签: java database client-server