【问题标题】:Java Client Server menuJava 客户端服务器菜单
【发布时间】:2020-02-17 16:44:33
【问题描述】:

所以我正在尝试制作一个 java 客户端服务器应用程序。在客户端,我有一个菜单供用户选择。然后我希望服务器做的是用适当的数据响应输入的选项。这是我的代码,我哪里出错了?(我也有一个用函数创建的玩家数据库类,但不需要包含来回答我的问题)

  1. 服务器类
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


    【解决方案1】:

    更改在客户端或服务器上打开输入/输出流的顺序。 如果您在一侧打开输入流,则当前线程将被阻塞,直到另一端打开输出流,反之亦然。 我想现在你的程序只是挂起。

    【讨论】:

    • 在客户端写入 DataOutputStream osToServer = new DataOutputStream(connectToServer.getOutputStream());之前 isFromServer = ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2013-07-03
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多