Java客户端服务器菜单
因此,我正在尝试制作一个java客户机-服务器应用程序。在客户端,我有一个带有选项的菜单供用户选择。然后,我希望服务器对输入的选项做出响应,并提供相应的数据。这是我的代码,哪里出了问题?(我还创建了一个带有函数的players数据库类,但不需要包含这些函数来回答我的问题)Java客户端服务器菜单,java,database,client-server,Java,Database,Client Server,因此,我正在尝试制作一个java客户机-服务器应用程序。在客户端,我有一个带有选项的菜单供用户选择。然后,我希望服务器对输入的选项做出响应,并提供相应的数据。这是我的代码,哪里出了问题?(我还创建了一个带有函数的players数据库类,但不需要包含这些函数来回答我的问题) 服务器类 在客户端或服务器上打开输入/输出流的更改顺序。 如果在一端打开输入流,当前线程将被阻塞,直到另一端的线程打开输出流,反之亦然。 我想现在您的程序就挂起了。在客户端写入DataOutputStream osToServ
在客户端或服务器上打开输入/输出流的更改顺序。 如果在一端打开输入流,当前线程将被阻塞,直到另一端的线程打开输出流,反之亦然。
我想现在您的程序就挂起了。在客户端写入DataOutputStream osToServer=newdataoutputstream(connectToServer.getOutputStream());在isFromServer=之前。。。
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
}}
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
}
}