使用socket进行网络编程的时候,客户端使用connect函数去连接服务器的时候,有时候会出现Connection refused的错误。
这种错误的两个原因(目前我所碰到的)是:
1.服务器没有启动相应的端口进行监听。
2.服务器存在防火墙,阻止了当前访问。
现对第二种情况进行解决:
1.在系统安装了类似于firewall等软件的情况下,根据对应的软件命令或者规则来进行调整
比如我们安装了firewall,可以使用
systemctl stop firewalld.service 命令来停止防火墙。
firewall-cmd --state 来查看防火墙的状态。
2.使用iptables:
iptables -F 清除所有的规则
iptables [-t table] [-AI chain] [-io interface] [-p protocol] [-s source] [-d destination] [--sport range] [--dport range] -j [action] 设置一个规则。[不针对nat 和 mangle]
其中各个占位符的取值为:
1.table : filter [-t 指定操作的表]
2.chain : INPUT OUTPUT [-AI 指定操作的链]
3.interface :自己的网络接口[可以通过 {`ip a`} 获得] [-io 注意这个是两个参数i/o 指定进出的网络接口]
4.protocol : tcp udp[-p 指定协议]
5.source :源ip [-s]
6.destination :目的ip[作为转发功能时,直接作为服务器应该用不到] [-d]
7.range :端口的范围{比如 1234:4321 表示1234端口到4321端口}[--sport/--dport]
8.action : ACCEPT/REJECT/DROP[-j 指定这条规则的动作,接受/拒绝/丢弃]