简体   繁体   English

Java-socket.isConnected使用端口80将true返回到任何IP地址

[英]Java - socket.isConnected returns true to ANY ip address using port 80

I am trying to establish communication with a device on my local network via the use of Socket , and verify the connection using the Socket.isConnected() method - seems simple enough, but I am getting some strange behavior. 我试图通过使用Socket与本地网络上的设备建立通信,并使用Socket.isConnected()方法验证连接-看起来很简单,但出现了一些奇怪的现象。

Currently, I instantiate an unconnected socket with the no-arg Socket() constructor, and then, when ready, connect to the server (which is on port 80 for the device). 当前,我使用no-arg Socket()构造函数实例化未连接的套接字,然后在准备就绪时连接到服务器(该设备位于端口80上)。 Here is a snippet of the code: 这是代码片段:

deviceSocket = new Socket();
...
SocketAddress deviceSocketAddress = new InetSocketAddress(hostName, portNum);
deviceSocket.connect(deviceSocketAddress);
if (deviceSocket.isConnected()){
     System.out.println("Connected to "+hostName+":"+portNum);
}

As mentioned, the portNum that I must connect to on the device is 80, but regardless of the hostName that is provided, it always returns true . 如前所述,我必须在设备上连接的portNum为80,但是无论所提供的hostName为何,它始终返回true If I give an ip address of 10.0.1.115 (which is the address of the device), I get the same result as when I provide 10.0.1.114, or even something bogus like 13.2.5.2. 如果我提供的IP地址为10.0.1.115(这是设备的地址),则得到的结果与提供10.0.1.114时的结果相同,甚至是类似13.2.5.2的虚假信息。

I've already done some research and have found that the Socket.isConnected() method will return true if the socket has ever been successfully connected, but that is not my issue, I've made sure that in each case I am starting with a brand-new unconnected socket. 我已经进行了一些研究,发现如果套接字已成功连接,则Socket.isConnected()方法将返回true ,但这不是我的问题,我确保在每种情况下我都从全新的未连接插座。

I decided to take my issues to telnet, in which I noticed something very peculiar. 我决定将自己的问题带到telnet,其中发现了一些非常奇怪的东西。 If the command telnet xxx.xxx.xxx.xxx 80 is issued in the command prompt, it always opens a blank page, which hangs for a while and then exits back to command prompt, rather than giving the appropriate error message (assuming the endpoint really doesn't exist) 如果在命令提示符处发出命令telnet xxx.xxx.xxx.xxx 80 ,它将始终打开空白页,该页会挂起一会儿,然后退出并返回命令提示符,而不是给出适当的错误消息(假设端点真的不存在)

Connecting to 13.2.5.2... Could not open connection to the host, on port 82: Connect failed

(Also, something else strange, using port 81 behaves the same way, it is only when I used 82, or another random port, that the connection failed) (此外,使用端口81的行为有些奇怪,只有当我使用82或另一个随机端口时,连接失败)

So I think I've stumbled upon an interesting issue, but we'll see soon enough... 所以我想我偶然发现了一个有趣的问题,但是很快就会看到...

Thanks in advance for any and all replies! 预先感谢您的所有答复!

-M -M

I cannot replicate this behavior but here's a guess. 我无法复制这种行为,但这是一个猜测。

Your firewall allows port 80 (http) and 81 (often used as a http alternate). 您的防火墙允许端口80(http)和81(通常用作http备用端口)。 These ports try, then fail to connect after a timeout. 这些端口会尝试,然后在超时后无法连接。 Other ports that are not open in the firewall (like 82) are rejected immediately. 防火墙中未打开的其他端口(例如82)将立即被拒绝。

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

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