来自客户端的用户输入不会进入服务器

时间:2015-12-09 12:44:06

标签: java file-io client-server bufferedreader printwriter

在我的客户端服务器应用程序中,客户端应该以给定的特殊格式输入一些命令(即vp(size, transport, days))。现在,如果客户端在错误的格式中输入了错误的命令,则可以要求用户再次输入(这是我如何在else中设置逻辑)。但在此之后,一旦客户端再次输入,程序就无法读取并发送命令。实际上当用户再次输入请求时没有任何反应。

请让我发布服务器端代码(如果它是neede。

客户端:

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 ");
        }

 }

    }

1 个答案:

答案 0 :(得分:1)

我认为问题出在你的服务器上,它只与第一个建立的连接通信。 在你的无限循环里面

...
    } 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,而是使用相同的连接。

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