Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与Java ApacheCommonsNet TelnetClient的行为不一致_Java_Sockets_Telnet_Apache Commons Net_Socket Timeout Exception - Fatal编程技术网

与Java ApacheCommonsNet TelnetClient的行为不一致

与Java ApacheCommonsNet TelnetClient的行为不一致,java,sockets,telnet,apache-commons-net,socket-timeout-exception,Java,Sockets,Telnet,Apache Commons Net,Socket Timeout Exception,我正在使用下面的代码进行telnet操作。问题是,如果我不使用setSoTimeout(),它每次都会成功地连接到Telnet服务器;但是,如果使用它,成功连接的频率将降低到50%。它的使用方式有什么问题吗 由于不同的原因,我实际上需要套接字抛出超时异常,因此我需要使用setSoTimeout。请建议如何有效地使用它 public class TelnetClientHandle { private TelnetClient telnet = new T

我正在使用下面的代码进行telnet操作。问题是,如果我不使用setSoTimeout(),它每次都会成功地连接到Telnet服务器;但是,如果使用它,成功连接的频率将降低到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());
                }
            }
        }
} 

感谢您提供的代码:)我也面临“setSoTimeout();”的问题。我尝试过不同的方法,但效果并不理想