我的Java服务器端没有从Java客户端读取更多的传入消息

时间:2015-03-31 12:21:27

标签: java client-server

我用Java创建简单的programm Server-Client 我的服务器是

public class Server {
    private static ServerSocket serverSocket;
private static Socket clientSocket;
private static BufferedReader bufferedReader;
private static String inputLine;
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
   try
    {
        serverSocket = new ServerSocket(5000);
        clientSocket = serverSocket.accept();
        // Create a reader
         bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        // Get the client message
        while((inputLine = bufferedReader.readLine()) != null)
        System.out.println(inputLine); //Print
            }catch(IOException e){
        System.out.println(e);
    }
}

} 客户端是:

public class Client {
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String hostName = "localhost";
    try (
            Socket echoSocket = new Socket(hostName, 5000);
            PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
            BufferedReader in= new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
        String userInput;
        while ((userInput = stdIn.readLine()) != null) {
            out.println(userInput);
            System.out.println("echo: " + in.readLine());
        }
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host " + hostName);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to " + hostName);
        System.exit(1);
    }

}

}

为什么我的服务器不能再读?当我调试程序我的程序时,它正在循环中而在服务器中..我不知道为什么

3 个答案:

答案 0 :(得分:1)

您的服务器似乎没问题。您的服务器无法读取多次的原因是客户端上的这一行:

System.out.println("echo: " + in.readLine());

客户端希望服务器给出答案,但您的服务器不会发回任何内容。所以删除上面的行或让服务器发送响应。

答案 1 :(得分:1)

您的客户端未向服务器发送任何新行。它试图从服务器读取响应。

  • System.out.println(“echo:”+ in.readLine()); *

您有两种方法可以解决此问题

1 - 将服务器中的响应发送回客户端 2 - 删除in.readLine()

祝你好运

答案 2 :(得分:1)

好吧现在我的服务器运行完全..现在我有一个客户端在android中,当我发送一条消息时,Server Programm显示消息,然后服务器终止> ..这是android程序中的程序当我推按钮

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                messsage = textField.getText().toString(); // get the text message on the text field
                textField.setText(""); // Reset the text field to blank
                SendMessage sendMessageTask = new SendMessage();
                sendMessageTask.execute();


            }
        });
    }
        private class SendMessage extends AsyncTask<Void, Void, Void> {

            @Override
            protected Void doInBackground(Void... params) {
                try {

                    client = new Socket("192.168.2.3", 5000); // connect to the server
                    printwriter = new PrintWriter(client.getOutputStream(), true);
                    printwriter.write(messsage); // write the message to output stream

                    printwriter.flush();
                    printwriter.close();
                  //  client.close(); // closing the connection

                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

        }