我无法修复此错误,我尝试了一切,并四处张望,但仍然无法做到。我认为这是setblocking,但不知道该怎么办。我已经待了几个小时了,请帮忙,因为这是明天的作业
确切的错误是:读取时出错:[WinError 10035]无法立即完成无阻塞的套接字操作。
那么它来自客户端第52行的异常?
客户代码:
import socket
import errno
import sys
HEADER_LEN= 10
IP="127.0.0.1"
PORT= 1337
yourUsername= input("Username: ")
s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((IP, PORT))
s.setblocking(False)
username= yourUsername.encode("utf-8")
usernameHeader= f"{len(username):<{HEADER_LEN}}".encode("utf-8")
s.send(usernameHeader + username)
while True:
mess= input(f"{yourUsername}> ")
if mess:
mess= mess.encode("utf-8")
messHeader= f"{len(mess):<{HEADER_LEN}}".encode("utf-8")
s.send(messHeader + mess)
try:
while True:
usernameHeader= s.recv(HEADER_LEN)
if not len(usernameHeader):
print("Connection has been closed by the server")
sys.exit()
usernameLen= int(usernameHeader.decode("utf-8").strip())
username= s.recv(usernameLen).decode("utf-8")
messHeader= s.recv(HEADER_LEN)
messLen= int(messHeader.decode("utf-8").strip())
message= s.recv(usernameLen).decode("utf-8")
print(f"{username} > {message}")
except IOError as e:
if e.errno != errno.EAGAIN or e.errno != errno.EWOULDBLOCK:
print("Error while reading: {}".format(str(e)))
sys.exit()
continue
except Exception as e:
print("Error while reading: ".format(str(e)))
sys.exit()
import socket
import select
HEADER_LEN= 10
IP= "127.0.0.1"
PORT= 1337
s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#AF_INET is for ipv4, SOCK_STREAM is for TCP
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#allows to reconnect
s.bind((IP, PORT))
s.listen()
socketList= [s]
clientList= {}
print(f"Searching for users on {IP}:{PORT}...")
def receiveMessage(clientSocket):
try:
messageHeader= clientSocket.recv(HEADER_LEN)
if not len(messageHeader): #if we didnt reveive data
return False
messageLength= int(messageHeader.decode("utf-8").strip())
return {"header": messageHeader, "data": clientSocket.recv(messageLength)}
except:
return False
while True:
readSockets, empty, exceptionSockets = select.select(socketList, [], socketList,)
#This has a read list, write list and error list
for notifiedSocket in readSockets:
if notifiedSocket == s:
clientSocket, clientAddress = s.accept()
user= receiveMessage(clientSocket)
if user is False: #someone disconnects
continue
socketList.append(clientSocket)
clientList[clientSocket]= user
print(f"New connection: {clientAddress[0]}:{clientAddress[1]} username: {user['data'].decode('utf-8')}")
else:
message= receiveMessage(notifiedSocket)
if message is False:
print(f"{clientList[notifiedSocket]['data'].decode('utf-8')} has disconnected")
socketList.remove(notifiedSocket)
del clientList[notifiedSocket]
continue
user= clientList[notifiedSocket]
print(f"Message from {user['data'].decode('utf-8')}: {message['data'].decode('utf-8')}")
for clientSocket in clientList:
if clientSocket != notifiedSocket:
clientSocket.send(user['header']+ user['data']+ message['header']+ message['data'])
for notifiedSocket in exceptionSockets:
socList.remove(notifiedSocket)
del clientList[notifiedSocket]