BufferedReader等待换行

时间:2013-05-17 19:21:22

标签: java telnet bufferedreader

package sample_server;

import java.io.*;
import java.net.*;
import java.security.*;

/**
 * Title:        Sample Server
 * Description:  A simple telnet server
 * @author A. Jones
 * @version 0.01
 */

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;

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

    public void run () {
      try {
        // Get input from the client
        BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
        PrintStream out = new PrintStream(server.getOutputStream());
        System.out.println("A user has connected to the server.");
        out.println("You have connected.");
        MenuSystemClass newMenu = new MenuSystemClass(in, out);
        newMenu.MainMenu(server);
      } catch (IOException ioe) {
        System.out.println("IOException on socket listen: " + ioe);
        ioe.printStackTrace();
      }
    }
}

第二档

/*
 * Menu System Class
 * Simple to use menu
 */
package sample_server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

/**
 * My Custom Menu Class
 * @author aaron
 */
public class MenuSystemClass {
    private BufferedReader input = null;
    private PrintStream output = null;

    MenuSystemClass(java.io.BufferedReader in, PrintStream out) {
        input  = in;
        output = out;
    }

    public void MainMenu(java.net.Socket server) throws IOException {
        // Set Run Boolean to true
        boolean running = true;
        // We switch the input to lowecase here and compare
        while(running) {
            output.println("A: Stuff ; V: Things ; W: More Stuff; S: Also Stuff ; D: Delete ; Q: Quit");
            // Initialize the scanner
            Scanner user_input = new Scanner(input);
            // Get the user input
            String decision = user_input.next();
            // Conver their decision to lowercase and compare to choices.
            switch (decision.toLowerCase()) {
                case "a":
                    output.println("This is not yet implemented.");
                    break;
                case "s":
                    output.println("This is not yet implemented.");
                    break;
                case "d":
                    output.println("This is not yet implemented.");
                    break;
                case "v":
                    output.println("This is not yet implemented.");
                    break;
                case "w":
                    output.println("This is not yet implemented.");
                    break;
                case "q":
                    server.close();
                    System.out.println("A connection has been terminated by the client.");
                    return;
                default:
                    output.println("You did not select a viable option.");
                    output.println("Try again.");
                    break;
            }
        }
    }
}

我正在尝试制作一个telnet服务器。您可以连接到服务器,但是当您这样做时;它要求您在开始正确接受输入之前按两次Enter键。如何强制BufferedReader跳过新行的必要性,这样一旦你加载服务器它就准备好了 -

我想要什么 -

欢迎使用服务器。 输入A或B或无论如何。 A&lt; - 用户输入 你打字了!

它在做什么 - 欢迎来到服务器。 用户必须按Enter键 用户必须按Enter键 输入A或B等等。 &lt; - 用户输入 你输入了A!

如何让BufferedReader立即准备好接受用户输入?

1 个答案:

答案 0 :(得分:0)

如果您不想读取行,请不要调用readLine(),也不要使用Scanner类。

BufferedReader有三个其他read()方法。使用它们。

如果您想从控制台输入字符,请使用Console类。