import socket
from _thread import *
import pickle
server = "192.168.0.171"
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 connection, Server Started")
def read_pos(str): # Get position
str = str.split(",")
return int(str[0]), int(str[1]), int(str[2]), int(str[3]), int(str[4]), int(str[5])
def make_pos(tup, n1, n2, n3): # Make a position
return str(tup[0]) + "," + str(tup[1]) + "," + str(n1) + "," + str(n2) + "," + str(n3)
pos = [(200, 225, 0, 0, 0), (750, 225, 0, 0, 0)]
def threaded_client(conn, player):
# Make sure it indeed connect
conn.send(str.encode(make_pos(pos[player])))
reply = ""
while True:
try:
# Amount of info receive = 2048
data = read_pos(conn.recv(2048).decode())
pos[player] = data
if player == 1:
reply = pos[0]
else:
reply = pos[1]
if not data:
print("Disconnected")
break
else:
print("Received: ", data)
print("Sending: ", reply)
conn.sendall(str.encode(make_pos(reply)))
except:
break
# No connection
print("Lost connection")
conn.close()
currentPlayer = 0
while True:
conn, addr = s.accept()
print("Connected to:", addr)
start_new_thread(threaded_client, (conn, currentPlayer))
currentPlayer += 1
回溯(最后一次调用):文件“C:\Users\HP\PycharmProjects\Online_minigame_01\server.py”,第 15 行,在 s.listen(2) 中 OSError: [WinError 10022] An invalid argument was provided
因此,我尝试制作一款在线 2 人太空飞船射击游戏。但这部分(服务器)甚至无法运行。我已经尝试了很多方法,但它仍然无法正常工作。我应该如何解决它?