初始化菜单时停止来自套接字的错误输入

时间:2013-05-17 14:47:44

标签: java input telnet

我正在构建我的第一个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;
            }
        }
    }
}

我不能为我的生活让它忽略初始通信发生的事情,只是等到用户实际有意输入一些输入。

1 个答案:

答案 0 :(得分:1)

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

255 - 标记协商序列的开始。

251 - 确认愿意谈判。

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

修改

当然,您可以忽略协商并简单地使用前两个字节。这可以通过例如完成。这样。

  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...