1

我正在使用下面的代码进行远程登录操作。问题是如果我不使用 setSoTimeout(),它每次都成功连接到 Telnet Server;但是,如果使用它,成功连接的频率会降低到 50%。它的使用方式有什么问题吗?

由于不同的原因,我实际上需要套接字抛出超时异常,所以我需要使用 setSoTimeout。请建议如何以有效的方式使用它。

        public class TelnetClientHandle {

            private TelnetClient telnet = new TelnetClient();
            private InputStream in;
            private PrintStream out;

            public TelnetClientHandle(String server, String user, String password) {

                try {
                    telnet.setDefaultTimeout(20000);
                    try {
                        telnet.connect(server, 23, sourceIP, 0);
                    } catch (Exception e) {
                        return_value = "Unable to establish Telnet Connection with Server";
                        return;
                        }
                }
            }

                    in = telnet.getInputStream();
                    out = new PrintStream(telnet.getOutputStream());

                    String read_value = readUntil("login", true);
                    System.out.println(read_value);
                    if (read_value.contains("login")) {
                        write(user);
                        read_value = readUntil("sword", false);
                        System.out.println(read_value);
                        write(password);
                    } else {
                        return_value = "Unable to establish Telnet Connection with Server";
                        return;
                    }

                    telnet.setSoTimeout(2000);
                    read_value = readUntil("ABCDEFGH", false);
                    System.out.println(read_value);
                    if ((read_value == null)
                            || ((read_value != null && !(read_value.toLowerCase()
                                    .contains("last login") || read_value
                                    .toLowerCase().contains("microsoft"))))) {
                        return_value = "Login error: Incorrect credentials or Server denied connection";
                        return;
                    }

                } catch (Exception e) {
                    return_value = "Exception occurred: " + e.getMessage();
                }
            }

            public String readUntil(String pattern, boolean checkForAvailableStream) {
                StringBuffer sb = null;
                try {
                    char lastChar = pattern.charAt(pattern.length() - 1);
                    sb = new StringBuffer();
                    char ch = (char) in.read();
                    while (true) {
                        sb.append(ch);
                        if (ch == lastChar) {
                            if (sb.toString().endsWith(pattern)) {
                                return sb.toString();
                            }
                        }
                        if ((checkForAvailableStream) && (in.available() > 0)) {
                            ch = (char) in.read();
                        } else if (!checkForAvailableStream) {

                            ch = (char) in.read();
                        } else
                            break;
                    }
                } catch (Exception e) {
                }
                return sb.toString();
            }

            public void write(String value) {
                try {
                    out.write((value + "\r\n").getBytes("CP1252"));
                    out.flush();
                } catch (Exception e) {                                        return_value = "Exception occurred in sending command to Server: "
                            + e.getMessage();
                }
            }

            public void disconnect() {
                try {
                    telnet.disconnect();
                } catch (Exception e) {
                    "Exception occurred: " + e.getMessage());
                }
            }
        }
} 
4

0 回答 0