python - 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
请帮助我了解为什么会这样。我尝试重新编写代码,但没有成功。
解决方案
推荐阅读
- android - 在后台启动第二个 Android Activity 以加载,然后再放到前台
- java - 在 Java 中格式化 LocalDate:“2020 年 3 月 26 日”的模式是什么
- kotlin - Kotlin 通用类使用继承
- c# - 存储的密码是否会破坏 ASP.NET CompareValidator?
- python - 为什么我的 Discord 机器人无法正确打开或存储 JSON 文件中的内容?
- c++ - 如何将单个对象转换为 boost::any_range?
- java - 使用 Quarkus 连接到 Azure Key Vault
- python - 旋转图像后如何跟踪像素位置?
- sql - SQL如何从所有客户中检索最后订购的2个列出的产品?
- python - 为什么 "lala" + int 需要转换为 str 而 "lala" * 2 不需要?