首页 > 解决方案 > OSError: [WinError 10022] 在 socket.listen(2) 中提供了一个无效参数

问题描述

我在 Visual Studio Code 上为服务器项目运行此服务器代码:

import socket
from _thread import *
import sys

server = "192.168.0.4"
port = 5555

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    s.bind((server, port))

except socket.error as e:
    str(e)

s.listen(2)

print("Waiting for connections...")
print("Server Started!")

def threaded_client(conn):
    conn.send(str.encode("Connected"))
    reply = ""
    while True:
        try:
            data = conn.recv(2048)
            reply = data.decode("utf-8")

            if not data:
                print("Disconnected!")
                break

            else:
                print("Received: ", reply)
                print("Sending: ", reply)

            conn.sendall(str.encode(reply))

        except:
            break

        print("Lost Connection!")
        conn.close()

while True:
    conn, addr = s.accept()
    print("Connected to: ", addr)

    start_new_thread(threaded_client, (conn,))

由于某种原因,我收到这样的错误:

Traceback (most recent call last):
  File "c:/Users/sande/Desktop/Vihaan/ThirdPartySoftware/Python/VisualStudiosCode/RPSOnline/server.py", line 16, in <module>
    s.listen(2)
OSError: [WinError 10022] An invalid argument was supplied

请帮助我了解为什么会这样。我尝试重新编写代码,但没有成功。

标签: pythoninvalid-argument

解决方案


推荐阅读