Python套接字OSError

时间:2018-08-28 16:55:08

标签: python python-3.x sockets

我有一个接受输入的文件

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.bind(('localhost', 8088))
serversocket.listen(5) # become a server socket, maximum 5 connections

while True:
    connection, address = serversocket.accept()
    recvd_data = connection.recv(4096)
    if len(recvd_data) > 0:
        data = pickle.loads(recvd_data)
        print(data)
        serversocket.shutdown(socket.SHUT_RDWR)

该脚本可以正确打印输出的数据,但是会引发此错误。我不明白,也无法找到解决方案。

Traceback (most recent call last):
  File "sending.py", line 21, in <module>
    connection, address = serversocket.accept()
  File "/usr/lib/python3.5/socket.py", line 195, in accept
    fd, addr = self._accept()
OSError: [Errno 22] Invalid argument

1 个答案:

答案 0 :(得分:1)

您有一个无限循环来接收数据,但是在收到第一批后关闭serversocket。这就是第二次接受呼叫失败的原因