为什么我的网络Java代码会在每一轮中跳过输出?

时间:2015-05-06 00:46:24

标签: java network-programming echo

我有一个线程化的java客户端/服务器对,它的行为非常奇怪。这是服务器端:

python3.3 myscript.py

这是我的客户代码:

public class sample_server {

  private static int port=4444, maxConnections=0;
  // Listen for incoming connections and handle them
  public static void main(String[] args) {


    int i=0;
    try{
      ServerSocket listener = new ServerSocket(port);
      Socket server;

      while((i++ < maxConnections) || (maxConnections == 0)){
        doComms connection;

        server = listener.accept();
        doComms conn_c = new doComms(server);
        Thread t = new Thread(conn_c);
        t.start();
      }
    } catch (IOException ioe) {
      System.out.println("IOException on socket listen: " + ioe);
      ioe.printStackTrace();
    }
  }

}

class doComms implements Runnable {
    private Socket server;
    private String line,input;

    doComms(Socket server) {
      this.server=server;
    }

    public void run () {

      input="";

      try {
        // Get input from the client
        DataInputStream in = new DataInputStream (server.getInputStream());
        PrintStream out = new PrintStream(server.getOutputStream());

        while((line = in.readLine()) != null && !line.equals(".")) {
          java.util.Date date = new java.util.Date();
          String timeString = String.valueOf((date.getTime()));

          input=input + line + timeString;
          out.println("I got:" + line + " " + timeString + "\n");
        }

        // Now write to the client

          System.out.println("Overall message is:" + input);
          out.println("Overall message is:" + input);

          server.close();

      } catch (IOException ioe) {   /* ETC BOILERPLATE */

当我运行此代码时,在客户端上它只在每隔一轮显示完整的“echo”行,如下所示:

public class sample_client {

    public static void main(String[] args) throws IOException {

        String serverHostname = new String ("127.0.0.1");

        if (args.length > 0)
           serverHostname = args[0];
        System.out.println ("Attemping to connect to host " +
            serverHostname + " on port 10007.");

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            // echoSocket = new Socket("taranis", 7);
            echoSocket = new Socket(serverHostname, 4444);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + serverHostname);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                               + "the connection to: " + serverHostname);
            System.exit(1);
        }

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

        System.out.print ("input: ");
    while ((userInput = stdIn.readLine()) != null) {
        out.println(userInput);
        System.out.println("echo: " + in.readLine());
            System.out.print ("input: ");
    }

    out.close();
    in.close();
    stdIn.close();
    echoSocket.close();
     //END OLD CODE

}
}

感谢

1 个答案:

答案 0 :(得分:1)

PrintStream out = new PrintStream(server.getOutputStream());

您也需要autoflush参数,就像在构建PrintWriter的客户端中一样。

或者,如果您感到困扰,请在服务器中的每个flush()后拨打println()