Telnet客户端连接被拒绝-JAVA

Telnet客户端连接被拒绝-JAVA,java,telnet,Java,Telnet,我正在尝试创建到全局IP地址的telnet客户端连接 如果我使用下面这样的东西,我可以建立连接 private TelnetClient telnet = new TelnetClient(); telnet.connect("172.xx.xxx.xx", port); 然而,写它的东西如下,我得到连接拒绝错误 private TelnetClient telnet = new TelnetClient(); String host = "172.xx.xxx.xx"; telnet.

我正在尝试创建到全局IP地址的telnet客户端连接

如果我使用下面这样的东西,我可以建立连接

private TelnetClient telnet = new TelnetClient();

telnet.connect("172.xx.xxx.xx", port);
然而,写它的东西如下,我得到连接拒绝错误

private TelnetClient telnet = new TelnetClient();

String host = "172.xx.xxx.xx";

telnet.connect(host, port);
有什么建议吗?我在论坛上找不到同样的错误,而且我也不擅长提问:

这是我的完整代码

public void connectionCreater(String host, int port,String uID,String pass,
                String account, String password) {  
            try {
                //telnet.connect("172.xx.xxx.xx", port); this is works. 
                telnet.connect(host, port);
                out = new PrintWriter(telnet.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(
                        telnet.getInputStream()));
                if (readUntilThenExecute("login: ", uID + "\r")) {
                    if (readUntilThenExecute("Password: ", pass + "\r")) {
                        if (readUntilThenExecute("Enter User Name", account)) {
                            if (readUntilThenExecute("Enter Password", password)) {
                //to do stuff           }
                        }
                    }
                }
            } catch (IOException e) {
                out.close();
                try {
                    in.close();
                } catch (IOException y) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        public boolean readUntilThenExecute(String word, String command) {
            try {
                String result1 = "";
                char[] incoming = new char[2048];
                boolean check = true;
                while (check) {
                    int lenght = in.read(incoming);
                    result1 = String.copyValueOf(incoming, 0, lenght);
                    System.out.println(result1);
                    if (result1.contains(word)) {
                        out.println(command);
                        check = false;
                    }

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
            return true;
        }       

1.在终端应用程序/附件/终端中使用此命令安装telnet:

sudo apt-get install xinetd telnetd
2.使用您喜爱的具有root权限的文件编辑器编辑/etc/inetd.conf,添加以下行:

telnet stream tcp nowait telnetd /usr/sbin/tcpd /usr/sbin/in.telnetd
telnet        23/tcp 
3.编辑/etc/xinetd.conf,使其内容如下所示:

# Simple configuration file for xinetd
#
# Some defaults, and include /etc/xinetd.d/
defaults
{
# Please note that you need a log_type line to be able to use log_on_success
# and log_on_failure. The default is the following :
# log_type = SYSLOG daemon info
instances = 60
log_type = SYSLOG authpriv
log_on_success = HOST PID
log_on_failure = HOST
cps = 25 30
}
# default: on
# description: The telnet server serves telnet sessions; it uses
# unencrypted username/password pairs for authentication.
service telnet
{
disable = no
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/in.telnetd
log_on_failure += USERID
}
4.您可以使用此行编辑/etc/services来更改telnet端口号:

telnet stream tcp nowait telnetd /usr/sbin/tcpd /usr/sbin/in.telnetd
telnet        23/tcp 
5.如果您对默认配置不满意。请编辑etc/xinetd.d/telnet,添加以下内容:

# Simple configuration file for xinetd
#
# Some defaults, and include /etc/xinetd.d/
defaults
{
# Please note that you need a log_type line to be able to use log_on_success
# and log_on_failure. The default is the following :
# log_type = SYSLOG daemon info
instances = 60
log_type = SYSLOG authpriv
log_on_success = HOST PID
log_on_failure = HOST
cps = 25 30
}
# default: on
# description: The telnet server serves telnet sessions; it uses
# unencrypted username/password pairs for authentication.
service telnet
{
disable = no
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/in.telnetd
log_on_failure += USERID
}
根据需要添加以下行:

only_from = 192.168.120.0/24 #Only users in 192.168.120.0 can access to
only_from = .bob.com #allow access from bob.com
no_access = 192.168.120.{101,105} #not allow access from the two IP.
access_times = 8:00-9:00 20:00-21:00 #allow access in the two times
......
6.使用此命令启动telnet服务器:

sudo /etc/init.d/xinetd start

您正在尝试连接什么以接受telnet连接?您的端口是否相同?当你重写代码时?是的,相同的端口。此代码用于向服务器发送一些命令/获取响应。此代码位于创建连接后的旁边。当我使用telnet.connect172.xx.xxx.xx时,端口;一切正常:/。out=新的PrintWritertelnet.getOutputStream,true;in=新的BufferedReadernew InputStreamReader telnet.getInputStream;发布完整的代码,在某个地方你有拼写错误,或者你在另一个环境/系统上使用了connecthost、port?谢谢你的关注,我解决了这个问题。我的同事更改了数据库主机记录,我只是在调试中看到…thx