服务器端与客户端连接中的问题

一、前言

本次计网作业要求写一个简单的文件传输系统,需要连接服务器端和客户端。在连接过程中遇到了一些问题,在此记录一下。

二、问题以及解决办法

1、服务器端报错:

(1)错误:

Traceback (most recent call last):
  File "/home/lighthouse/serveragreement/server.py", line 13, in <module>
    server_socket.bind((SERVER_IP, SERVER_PORT))
OSError: [Errno 99] Cannot assign requested address

显示连接不到我的服务器。

(2)解决方法:

在我的服务器端代码中,我规定的ip地址是外网地址,导致服务器找不到自己。此处应该更改为ip的内网地址;或者改为0.0.0.0,如果是这样的话,服务器上的所有ip都会被监听。

2、客户端报错

(1)错误:

解决完服务器端的错误,开始运行客户端,没意外的,又是一大堆错误

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 174, in _new_conn
    conn = connection.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 96, in create_connection
    raise err
  File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 86, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 716, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 417, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 244, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/usr/lib/python3.11/http/client.py", line 1298, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.11/http/client.py", line 1344, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.11/http/client.py", line 1293, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.11/http/client.py", line 1052, in _send_output
    self.send(msg)
  File "/usr/lib/python3.11/http/client.py", line 990, in send
    self.connect()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 205, in connect
    conn = self._new_conn()
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f7758e1a0d0>: Fai

(2)解决方法:

看到这一堆不知所云的东西,于是转而验证端口是否连通:
 

telnet 服务器地址 端口号

结果显示:
 

telnet: Unable to connect to remote host: 拒绝连接

情况已经很明了了,刚刚的一大堆错误是因为远端的服务器拒绝与我相连。

分析原因:可能是服务器端代码指定的端口被防火墙挡住了,下边开始尝试开放端口(我的端口是8888)

lighthouse@VM-24-15-ubuntu:~$ sudo iptables --append INPUT --protocol tcp --dport 8888 --jump ACCEPT

保存更改:
 

lighthouse@VM-24-15-ubuntu:~$ sudo iptables-save > rules.v4

再次尝试,发现连通成功。

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值