Python套接字问题!(错误:错误号10035)
我正在尝试使用套接字创建一个游戏。每个玩家都可以在服务器和客户端之间进行选择。下面是我的代码。如果有人能帮我连接插座,我将不胜感激。尝试将客户端连接到服务器时遇到错误10035。我花了很多时间试图解决这些问题,但都没有成功 更新:当我使用Python套接字问题!(错误:错误号10035),python,sockets,tcp,Python,Sockets,Tcp,我正在尝试使用套接字创建一个游戏。每个玩家都可以在服务器和客户端之间进行选择。下面是我的代码。如果有人能帮我连接插座,我将不胜感激。尝试将客户端连接到服务器时遇到错误10035。我花了很多时间试图解决这些问题,但都没有成功 更新:当我使用print(client.connect_ex((connection[1],65355))而不是client.connect和client.sendto在阶段=[“client”,“search”]下时,仍然出现错误10035。我更新了代码以反映我当前拥有的内
print(client.connect_ex((connection[1],65355))
而不是client.connect
和client.sendto
在阶段=[“client”,“search”]
下时,仍然出现错误10035。我更新了代码以反映我当前拥有的内容
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
导入pygame,套接字
#预设
pygame.init()
大小=(pygame.display.Info().current\u w,pygame.display.Info().current\u h)
screen=pygame.display.set_模式(大小,pygame.resizeable)
#screen=pygame.display.set_模式(大小,pygame.FULLSCREEN)
pygame.display.set_标题(“字形战争”)
clock=pygame.time.clock()
结束=错误
连接=[“”,“”]
IPNotFound=False
font=pygame.font.SysFont(“纸莎草纸”,30,假,假)
单击=False
阶段=[“菜单”,“”]
#功能
def x(刻度):
返回大小[0]*刻度
def y(刻度):
返回大小[1]*刻度
def显示按钮(文本、输出、x、y、w、h、颜色、tx、ty、单击):
pygame.draw.rect(屏幕,颜色,[x,y,w,h])
pygame.draw.rect(屏幕,(0,0,0),[x,y,w,h],5)
blit(font.render(text,False,(0,0,0)),[x+tx,y+ty])
如果(mpos[0]>x和mpos[0]y和mpos[1]
你应该看看这里
但您应该首先澄清每个错误触发的位置/时间。
另外,我强烈建议您为客户机/服务器套接字编写一些简化的代码,您应该可以找到大量的教程和博客来介绍这一点
PS:看起来您正在使用TCP(套接字流),在尝试使用TCP进行面向消息的交换时有一些注意事项,UDP可能更适合您的需要。(否则,您应该在发送数据时定义流结束标记)
引述: WSAEINVAL 10022 参数无效。 提供了一些无效的参数(例如,为setsockopt函数指定了无效的级别)。在某些情况下,它还引用套接字的当前状态,例如,在未指定的套接字上调用accept