0

我正在构建我的第一个 telnet 服务器。我可以连接到它,但由于某种原因,它在第一次连接时自动在菜单上失败。连接后,它会立即显示欢迎消息,然后立即指出我的第一个输入不是一个可行的选择,然后再试一次。然后它可以让我正确地做出下一个选择。

如何防止java看到任何初始输入并等到用户实际按下一个键开始接受用户的输入?

我的代码——

public class Sample_server {

  private static int port=4444, maxConnections=5;
  // 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());
        out.println("You have connected to the server.");
        out.println("Welcome.");
        try {
            MenuSystemClass newMenu = new MenuSystemClass();
            newMenu.MainMenu(in, out);
        } catch (Exception e) {
            System.out.println("Failed to start the menu");
        }

        // Now write to the client
        System.out.println("Overall message is:" + input);

        server.close();
      } catch (IOException ioe) {
        System.out.println("IOException on socket listen: " + ioe);
        ioe.printStackTrace();
      }
    }
}

我的菜单代码

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sample_server;

import java.io.DataInputStream;
import java.io.PrintStream;
import java.util.Scanner;

/**
 * My Custom Menu Class
 * @author aaron
 */
public class MenuSystemClass {

    public void MainMenu(DataInputStream in, PrintStream out) {
        // Set Run Boolean to true
        boolean running = true;
        // We switch the input to lowecase here and compare
        while(running) {
            out.println("A: New Char ; V: View ; W: Start Game; S: Save ; D: Delete ; Q: Quit");
            // Initialize the scanner
            Scanner user_input = new Scanner(in);
            // Get the user input
            String decision = user_input.next();
            // Conver their decision to lowercase and compare to choices.
            switch (decision.toLowerCase()) {
                case "a":
                    out.println("This is not yet implemented.");
                    break;
                case "s":
                    out.println("This is not yet implemented.");
                    break;
                case "d":
                    out.println("This is not yet implemented.");
                    break;
                case "v":
                    out.println("This is not yet implemented.");
                    break;
                case "w":
                    out.println("This is not yet implemented.");
                    break;
                case "q":
                    return;
                default:
                    out.println("You did not select a viable option.");
                    out.println("Try again.");
                    break;
            }
        }
    }
}

我这辈子都无法忽略初始通信正在发生的事情,而只是等到用户真正有目的地输入一些输入。

4

1 回答 1

1

telnet 客户端在协商开始时发送一些字节。在我的情况下(当我测试你的代码时)它是

255 - 标志着协商序列的开始。

251 - 确认愿意谈判。

您的扫描仪会消耗它,这就是您的问题所在。你可能需要处理谈判,所以这是我找到的描述......

编辑

当然,你可以忽略协商,直接消耗前两个字节。这可以例如以这种方式完成。

  try {
    // Get input from the client
    DataInputStream in = new DataInputStream (server.getInputStream());
    PrintStream out = new PrintStream(server.getOutputStream());
    out.println("You have connected to the server.");
    out.println("Welcome.");
    in.readLine(); //consume first two bytes
    //the rest of your code...
于 2013-05-17T15:20:30.637 回答