简体   繁体   English

如何检查惠特指定端口是否可以通信?

[英]How to check wheather specified port is available for communication?

I am using Java Secure Channel to connect with hosts in a network. 我正在使用Java安全通道与网络中的主机连接。 I am using following code: 我正在使用以下代码:

int port = 22;JSch jsch = new JSch();
try {
session=jsch.getSession(username,hostname,port);
}
catch (JSch Exception e1){
e1.printStackTrace();
}
session.setPassword("mypassword");
try {
session.connect();
}catch (JSchException e1){
System.out.println("Did not connect");
return;
}

By using this code I am able to connect to host. 通过使用此代码,我可以连接到主机。 But if host is unavailable/offline then I have to wait for JSchException. 但是,如果主机不可用/离线,那么我必须等待JSchException。

1) I want to first check whether host is available for communication or not and if available then only try to connect. 1)我想先检查主机是否可用于通讯,如果可用,则仅尝试连接。

2) Also I want to set timeout for connection,if connection doesn't established within that time interval must give exception. 2)我也想设置连接超时,如果在该时间间隔内未建立连接,则必须给出异常。

How to check if host is available? 如何检查主机是否可用? and set timeout for connection? 并设置连接超时?

Testing before connecting makes no big sense, as port can become unavailable right after test. 连接之前进行测试没有太大意义,因为端口在测试之后可能会变得不可用。 Try to use session.setTimeout() to set timeout in millis. 尝试使用session.setTimeout()以毫秒为单位设置超时。

How to check whether specified port is available for communication? 如何查看指定的端口是否可以通信?

How to check whether specified X is available for Y: 如何检查指定的X是否可用于Y:

Try to use it for Y. It's an infallible test. 尝试将其用于Y。这是一个可靠的测试。 Any other technique has timing-window problems. 任何其他技术都具有时序窗口问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM