因此,我正在尝试制作Java客户端服务器应用程序。在客户端,我有一个菜单,带有供用户选择的选项。然后,我希望服务器执行的操作是使用适当的数据响应输入的选项。这是我的代码,我要去哪里了?(我还有一个使用函数创建的players数据库类,但无需包括回答我的问题)
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
}
}
更改在客户端或服务器上打开的输入/输出流的顺序。如果在一侧打开输入流,则当前线程将被阻塞,直到在另一端打开输出流,反之亦然。我想现在您的程序刚刚挂起。