Java - socket.isConnected 使用端口 80 向任何 ip 地址返回 true [英] Java - socket.isConnected returns true to ANY ip address using port 80

查看:32
本文介绍了Java - socket.isConnected 使用端口 80 向任何 ip 地址返回 true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用 Socket 与本地网络上的设备建立通信,并使用 Socket.isConnected() 方法验证连接 - 似乎很简单,但我有一些奇怪的行为.

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.

目前,我使用 no-arg Socket() 构造函数实例化一个未连接的套接字,然后在准备好后连接到服务器(位于设备的端口 80).下面是代码片段:

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). 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);
}

如前所述,我必须在设备上连接的 portNum 是 80,但无论提供的 hostName 如何,它总是 返回 true.如果我提供 10.0.1.115 的 IP 地址(这是设备的地址),我得到的结果与我提供 10.0.1.114 时的结果相同,甚至是像 13.2.5.2 这样的虚假地址.

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. 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.

我已经做了一些研究,发现 Socket.isConnected() 方法将返回 true 如果套接字曾经成功连接过,但那是不是我的问题,我已经确保在每种情况下我都从一个全新的未连接的套接字开始.

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.

我决定将我的问题提交到 telnet,在那里我注意到一些非常奇怪的事情.如果在命令提示符下发出命令 telnet xxx.xxx.xxx.xxx 80,它总是打开一个空白页面,该页面挂了一段时间然后退出回到命令提示符,而不是给出适当的错误消息(假设端点确实不存在)

I decided to take my issues to telnet, in which I noticed something very peculiar. 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)

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

(还有一点很奇怪,使用端口 81 的行为方式相同,只有当我使用 82 或其他随机端口时,连接才会失败)

(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)

所以我想我偶然发现了一个有趣的问题,但我们很快就会看到...

So I think I've stumbled upon an interesting issue, but we'll see soon enough...

预先感谢所有回复!

-M

推荐答案

我无法复制此行为,但这是一个猜测.

I cannot replicate this behavior but here's a guess.

您的防火墙允许端口 80 (http) 和 81(通常用作 http 替代).这些端口尝试,然后在超时后无法连接.其他没有在防火墙中打开的端口(如 82)会立即被拒绝.

Your firewall allows port 80 (http) and 81 (often used as a http alternate). These ports try, then fail to connect after a timeout. Other ports that are not open in the firewall (like 82) are rejected immediately.

这篇关于Java - socket.isConnected 使用端口 80 向任何 ip 地址返回 true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆