Я пытаюсь создать игру, используя сокеты. Каждый игрок может выбирать между сервером и клиентом. Ниже мой код. Если кто-нибудь может помочь мне подключить розетки, это будет очень признателен. Я сталкиваюсь с ошибкой 10035 при попытке подключить клиент к серверу. Я потратил много часов, пытаясь решить эти проблемы, но я был крайне неудачным.
ОБНОВЛЕНИЕ: ошибка 10035 по-прежнему появляется, когда я использую print(client.connect_ex((connection[1], 65355))
вместо client.connect
и client.sendto
в phase = ["client", "searching"]
. Я обновил код, чтобы отразить то, что у меня есть.
import pygame, socket
#Presets
pygame.init()
size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
#screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
pygame.display.set_caption("Glyph Wars")
clock = pygame.time.Clock()
end = False
connection = ["", ""]
IPNotFound = False
font = pygame.font.SysFont("Papyrus", 30, False, False)
click = False
phase = ["menu", ""]
#Functions
def x(scale):
return size[0] * scale
def y(scale):
return size[1] * scale
def display_button(text, output, x, y, w, h, color, tx, ty, click):
pygame.draw.rect(screen, color, [x, y, w, h])
pygame.draw.rect(screen, (0, 0, 0), [x, y, w, h], 5)
screen.blit(font.render(text, False, (0, 0, 0)), [x + tx, y + ty])
if (mpos[0] > x and mpos[0] < x + w) and (mpos[1] > y and mpos[1] < y + h) and click == True:
phase[0] = output[0]
phase[1] = output[1]
click = False
def display_text(text, x, y, color):
screen.blit(font.render(text, False, color), [x, y])
while end == False:
#Presets
mpos = pygame.mouse.get_pos()
screen.fill((90, 90, 90))
#Controls
for event in pygame.event.get():
if event.type == pygame.QUIT:
end = True
elif event.type == pygame.MOUSEBUTTONDOWN:
click = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.display.iconify()
if phase == ["client", "typing"]:
if event.key != pygame.K_BACKSPACE and event.key != pygame.K_RETURN:
connection[1] += event.unicode
elif event.key == pygame.K_BACKSPACE:
connection[1] = connection[1][:-1]
elif event.key == pygame.K_RETURN:
IPNotFound = False
phase = ["client", "initiating"]
#Logic
if phase == ["menu", ""]:
display_button("Host Game", ["host", "initiating"], x(0.4), y(0.3), int(x(0.2)), int(y(0.05)), (10, 240, 210), x(0.06), y(0.005), click)
display_button("Join Game", ["client", "typing"], x(0.4), y(0.4), x(0.2), y(0.05), (10, 240, 210), x(0.06), y(0.005), click)
elif phase[0] == "host":
if phase[1] == "initiating":
server = socket.socket()
server.setblocking(True)
server.settimeout(1)
serverlink = (socket.gethostbyname(socket.gethostname()), 65355)
server.bind(serverlink)
server.listen(3)
phase[1] = "searching"
elif phase[1] == "searching":
display_text("Searching for players...", x(0.4), y(0.2), (0, 0, 0))
display_text("IP: " + server.getsockname()[0] + ":" + str(server.getsockname()[1]), x(0.4), y(0.3), (255, 0, 0))
try:
connection[0], connection[1] = server.accept()
connection[0].setblocking(True)
server.sendto("connected".encode(), connection[1])
phase[1] = "connected"
except socket.timeout:
pass
elif phase[1] == "connected":
display_text("Connected with " + connection[1], x(0.4), y(0.4), (0, 255, 0))
elif phase[0] == "client":
display_button(connection[1], ["client", "typing"], x(0.4), y(0.3), x(0.2), y(0.05), (130, 130, 130), x(0.005), y(0.005), click)
if connection[1] == "":
display_text("Enter IP", x(0.405), y(0.305), (65, 65, 65))
if IPNotFound == True:
display_text("Having difficulty finding IP...", x(0.4), y(0.4), (0, 0, 0))
if phase[1] == "initiating":
client = socket.socket()
client.setblocking(True)
client.settimeout(5)
phase[1] = "searching"
elif phase[1] == "searching":
phase[1] = "typing"
try:
#print(client.connect_ex((connection[1], 65355)))
client.connect((connection[1], 65355))
client.sendto("connected".encode(), (connection[1], 65355))
IPNotFound = False
phase[1] = "connected"
except socket.timeout:
IPNotFound = True
elif phase[1] == "connected":
display_text("Connected with " + connection[1], x(0.4), y(0.4), (0, 255, 0))
#End
click = False
pygame.display.flip()
clock.tick(60)
pygame.quit