gpt4 book ai didi

python - 我的服务器(Python套接字)有什么问题?

转载 作者:行者123 更新时间:2023-12-03 12:07:10 25 4
gpt4 key购买 nike

我启动了一个旨在通过套接字制作多人井字游戏的项目。在该项目的第一步中,我已经对服务器和客户端进行了编码,以便播放器可以连接到服务器。但是由于某种原因,服务器仅接受一个连接。
因此,如果我打开服务器,并连接一个客户端,则服务器将执行我编写的程序,但是如果我依次连接其他客户端,则服务器将不执行任何操作。似乎服务器仅应用我对第一个连接所做的操作。
我真的不知道这个问题的根源。
有我的client.py:

import socket
import time

class Client(object):

def __init__(self, ip="192.168.0.15", port=5000):
self.ip = ip
self.port = port
self.address = (self.ip, self.port)

def start(self):
self.client_manager()

def client_manager(self):
client_socket = self.connect_to_server()

try:
client_username = str(input(">> Enter your username: "))
client_socket.send(client_username.encode("UTF-8"))
print("You're welcome, {}! You've connected successfully to server.".format(client_username))
players_quantity = int(client_socket.recv(1024).decode("UTF-8"))

while players_quantity == 1:
self.check_players_quantity(players_quantity, client_socket)
time.sleep(2)
except:
print("An error ocurred on client.")

def check_players_quantity(self, players_quantity, client_socket):
if players_quantity == 1:
print("You've connected to server. One player remaining, please wait...")
elif players_quantity == 2:
print("You've connected to server. All two players are ready, waiting for server...")
client_socket.send("READY".encode("UTF-8"))

def connect_to_server(self):
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(self.address)

return client_socket

if __name__ == "__main__":
client = Client()
client.start()
还有我的server.py:
import socket
import threading

class Server(object):

def __init__(self, ip="192.168.0.15", port=5000):
self.ip = ip
self.port = port
self.address = (self.ip, self.port)
self.clients_list = []
self.users_list = []

def start(self):
self.receive_connection()

def server_manager(self, client_socket):
client_username = client_socket.recv(1024).decode("UTF-8")
self.users_list.append(client_username)
print("{} has connected to server.".format(client_username))

players_quantity = str(len(self.clients_list))
client_socket.send(players_quantity.encode("UTF-8"))

received_data = client_socket.recv(1024).decode("UTF-8")

if received_data == "READY":
print("All two players are ready. Type 'start' to start the game.")
data = str(input(">> "))

def receive_connection(self):
server_socket = self.initiliaze_socket()

try:
while True:
client_socket, address = server_socket.accept()
self.clients_list.append(client_socket)
print("Connection received from '{}:{}'.".format(address[0], address[1]))
self.server_manager(client_socket)
except:
print("An error ocurred on server!")

def initiliaze_socket(self):
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(self.address)
server_socket.listen()

print("Server is running on '{}:{}'. Waiting for connections.".format(self.address[0], self.address[1]))

return server_socket

if __name__ == "__main__":
server = Server()
server.start()
Screenshot when I turn on the server:
Screenshot when the first client connects to server
Screenshot when the second client connects to server

最佳答案

看起来您在服务器代码上导入了threading,但从未使用过。为了从您的服务器为多个客户端提供服务,您将需要为每个连接创建一个新线程,或者使用异步IO。
您可以尝试通过更改线程方法

self.server_manager(client_socket)
在receive_connection中
threading.Thread(target=self.server_manager, args=(client_socket,)).start()

关于python - 我的服务器(Python套接字)有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65902812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com