背景
我们经常会遇到这样的问题:服务已经启动了,端口也监听了,但是连接不上。
说明
Local Address,意为本地地址的意思。
Local Address定义了传入的连接请求(即tcp SYN数据包)将被哪个网卡接收(因为电脑上一般都有eth0和lo至少2个网卡)。
如果值为0.0.0.0,表示传入的连接请求将被任意一个网卡接收。
测试
在服务器A(ip:10.123.16.20)上,分别有三个启动的服务,用netstat -l -n -t 命令查看:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:9600 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8600 0.0.0.0:* LISTEN
tcp 0 0 10.123.16.20:8024 0.0.0.0:* LISTEN
说明:服务器A的网卡情况:
从另外一台机器B上请求这些服务:
telnet 10.123.16.20 9600
Trying 10.123.16.20...
telnet: connect to address 10.123.16.20: Connection refused
telnet: Unable to connect to remote host: Connection refused
telnet 10.123.16.20 8024
Trying 10.123.16.20...
Connected to gzhxy-ns-map-innet-bee01.xxx.xxx.com (10.123.16.20).
Escape character is '^]'.
telnet 10.123.16.20 8600
Trying 10.123.16.20...
Connected to gzhxy-ns-map-innet-bee01.xxx.xxx.com (10.123.16.20).
即 B到端口8600、8024的网络是通的,到端口9600网络不通。
按照前面的定义:
Local Address (0.0.0.0:8600)代表任意一个网卡,都可以接收请求端口8600的数据包
Local Address(10.123.16.20:8024)代表只有第二个网卡,可以接收请求端口8024的数据包
Local Address(127.0.0.1:9600) 代表只有第一个网卡(lo,只能处理目标地址是127.0.0.1或者Localhost的网络包),可以接收请求端口9600的数据包。
故从机器B请求9600端口,肯定是不通的。(数据B请求A,数据包必须经过第二个网卡)
接着,在机器A上做测试:
telnet 10.123.16.20 9600
Trying 10.123.16.20...
telnet: connect to address 10.123.16.20: Connection refused
telnet: Unable to connect to remote host: Connection refused
telnet 127.0.0.1 9600
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1)
telnet 10.123.16.20 8024
Trying 10.123.16.20...
Connected to gzhxy-ns-map-innet-bee01.gzhxy.xxx.xxx (10.123.16.20).
telnet 127.0.0.1 8024
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host: Connection refused
这个结果就不用解释了。