java - 来自客户端的用户输入不会发送到服务器

标签 java file-io client-server bufferedreader printwriter

在我的客户端服务器应用程序中,客户端应该以给定的特殊格式输入一些命令(即vp(大小,传输,天数))。现在,如果客户端以错误的格式输入错误的命令,它可以要求用户再次输入(这就是我在 else 中设置逻辑的方式)。但此后,一旦客户端再次输入,程序将无法读取和发送命令。实际上当用户再次输入请求时什么也没有发生。

如果需要,请让我发布服务器端代码。

客户:

public class TCPClient {

    static boolean run = true;

    public static void main(String args[]) {
        Socket client = null;
        int portnum = 8080;
        if (args.length >= 1) {
            portnum = Integer.parseInt(args[0]);
        }
        new TCPClient(client, portnum);
    }

    public TCPClient(Socket client, int portNumber) {
        try {
            String answer = "";
            String command = "";

            // Creating client socket
            client = new Socket(InetAddress.getLocalHost(), portNumber);
            System.out.println("Client socket is made" + client);

            // Creating output stream
            OutputStream clientOut = client.getOutputStream();
            PrintWriter pw = new PrintWriter(clientOut, true);

            InputStream clientIn = client.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));

            BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));

            while (run) {



                System.out.println("Mention the Family Size (FZ), Means of Travel (MT), Number of Days (ND) in the given format below: "
                        + "vp(FZ,MT,ND)");//example: vp(2,air,30,)

                command = userInput.readLine().trim(); //sending a request to the server
                pw.println(command);

                answer = br.readLine();     //reading the response from the server

                System.out.println("Server replied: " + answer);
 if (answer != null && answer.startsWith("Price")) {
                    System.out.println("Write your Name (N), ID number (ID) and Phone Number (PN) in the format: "
                            + "PersonDetalis(N,ID,PN)");//

                } else {

                    System.out.println("Program initializing.. Try again!");
                    new TCPClient(client, portNumber);

                }

                command = userInput.readLine().trim();//sending request
                pw.println(command);

                System.out.println("Server replied: " + br.readLine());
                run = false;
            }
            pw.close();
            br.close();
            client.close();
        } catch (IOException ie) {

            System.out.println("I/O error. Run the program again ");
        }

 }

    }

最佳答案

我认为问题出在您的服务器中,该服务器仅与第一个建立的连接进行通信。 在你的无限循环中

...
    } else {
        System.out.println("Program initializing.. Try again!");
        new TCPClient(client, portNumber);
    }
...

每次创建新连接(套接字)时,您都必须在服务器上接受该连接。 我写了自己的简单服务器

ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
    Socket clientSocket = serverSocket.accept();
    PrintWriter out =
            new PrintWriter(clientSocket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(
            new InputStreamReader(clientSocket.getInputStream()));
    String line = in.readLine();
    System.out.println("SERVER: received " + line);
    out.println("server error");
    out.close();
    in.close();
    clientSocket.close();
}

并且成功了。

<小时/>

我建议您将复杂的逻辑从 TCPClient 构造函数中移走,并在 System.out.println("Program initializing.. Try again!"); 之后停止每次创建新的 TCPClient并使用相同的连接。

但是如果您这样做,它将不适用于我的服务器代码示例,因为我的服务器在收到某些内容并发回回复后立即关闭连接。

关于java - 来自客户端的用户输入不会发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34179144/

相关文章:

java - 如何使用 BindBeans 将字符串列表输入到 SQL

java - 如何在我的 JFrame 中调整这些面板的大小?

c - 一次从文件中读取两行

http - 多个客户端如何同时连接到服务器上的一个端口,比如 80?

java.lang.ClassCastException : java. io.ObjectStreamClass 无法转换为 [MyClass]

java - 从 react 堆级别运行时的 Maven 强制执行器问题

java.util.Date 无法转换为 java.sql.Date

c++ - 文件输入不循环遍历文件

ruby - 如何使用ruby获取目录中的文件数

c++ - 在 C++ 中读取未知编码的文本文件