10

我正在处理这段代码

from socket import *
HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(2)

while 1:
    print ("waiting on connection")
    clientsock, addr = serversock.accept()
    print ('connected from:', addr)
    while 1:
        data = clientsock.recv(1024).decode()
        if not data: break 
        clientsock.send(data.encode())
        clientsock.close()

serversock.close()

我收到此错误:

OSError: [WinError 10038] an operation was attempted on something that is not a socket
4

2 回答 2

22

仅读取部分数据后,您正在关闭客户端。

clientsock.close()

是在错误的缩进级别。将其向左移动一步。

于 2013-03-04T20:12:41.223 回答
1

问题在于close()。连接关闭后,您需要再次初始化连接

在while循环中添加这一行

serversock = socket(AF_INET, SOCK_STREAM)
于 2021-03-30T14:51:05.903 回答