Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 程序挂起,可能是基于套接字的_Python_Sockets_Networking_Pygame - Fatal编程技术网

Python 程序挂起,可能是基于套接字的

Python 程序挂起,可能是基于套接字的,python,sockets,networking,pygame,Python,Sockets,Networking,Pygame,所以我得到了这个.py文件,它被主文件调用,将程序发送到“主机服务器并等待有人连接到它”模式,它工作得很好,但是如果你退出,然后返回,程序就会冻结 #data.host #the host with the most #import modules import os, sys, pygame #basic stuff from pygame.locals import * #fiddly bits import socket from data.graphics import Letter,

所以我得到了这个.py文件,它被主文件调用,将程序发送到“主机服务器并等待有人连接到它”模式,它工作得很好,但是如果你退出,然后返回,程序就会冻结

#data.host
#the host with the most

#import modules
import os, sys, pygame #basic stuff
from pygame.locals import * #fiddly bits
import socket
from data.graphics import Letter,Flicker,BackButton,FlickerHandler
import config as conf
import threading
from data.settings import *

def net_srv(): #don't ask me, look it up
    global sock, connected

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('', port))
    sock.listen(1); connected = False

class HostScreen():
    def __init__(self):
        pass
    def enter(self, client):
        conf.state = conf.STATE_HOSTING
        size = (width,height) = [640,480]
        screen = pygame.display.set_mode(size)
        dt = 0.0
        clock = pygame.time.Clock()
        background = pygame.Surface(screen.get_size())
        background = background.convert()
        background.fill((0, 0, 0))
        font = pygame.font.Font(os.path.join("data","graphics","lucon.ttf"), 11)
        text = font.render("Waiting for other player to connect.", 1, (255, 255, 255))
        textpos = text.get_rect()
        textpos.center = background.get_rect().center
        textpos.centery -= 10
        f = Letter("f")
        l = Letter("l")
        i = Letter("i")
        c = Letter("c")
        k = Letter("k")
        e = Letter("e")
        r = Letter("r")
        flicker = Flicker()
        handler = FlickerHandler()
        back = BackButton()
        allsprites2 = (f,l,i,c,k,e,r)
        clickables = [back]
        for s in allsprites2:
            s.update(1)
            s.rect.centery = s.rect.centery - 50
        allsprites = pygame.sprite.LayeredDirty(allsprites2,flicker,clickables)
        flicker.update(1)
        timetoFlicker = 0.5
        titleSwitch = 0
        net_srv()

        def connect():
            global conn, addr, connected
            conn, addr = sock.accept()
            connected = True

        thread = threading.Thread(target=connect)
        thread.start()

        while 1:
            dt = (clock.tick(60)/1000.0)
            timetoFlicker -= dt
            pygame.display.set_caption("Flicker  %d fps" % clock.get_fps())
            events = pygame.event.get()
            for event in events:
                if event.type == pygame.QUIT: sys.exit() #Makes the X button work.
                if event.type == MOUSEBUTTONDOWN: #Checking for clicking.
                    if event.button == 1:
                        for object in clickables:
                            if object.rect.collidepoint(pygame.mouse.get_pos()) and object.clickCheck() == "back":
                                flicker.update(1)
                                timetoFlicker = 0.09
                                titleSwitch = 1
            if connected: return (conn, addr, 'server')
            elif not thread.is_alive():
                flicker.update(1)
                timetoFlicker = 0.09
                titleSwitch = 1
            if timetoFlicker<=0:
                timetoFlicker,vis = handler.flicker(flicker)
                flicker.update(vis)
                for object in clickables:
                    object.update(vis)
                if titleSwitch > 0 and vis == 0:
                    timetoFlicker = 0.02
                elif titleSwitch > 0:
                    sock.close()
                    return False
            pygame.event.pump()
            if not flicker.visible:
                screen.blit(text, textpos)
            rects = allsprites.draw(screen)
            pygame.display.update(rects)
            pygame.display.flip()
#data.host
#最受欢迎的主持人
#导入模块
导入操作系统、系统、pygame#基本内容
从pygame.locals导入*#精细位
导入套接字
从data.graphics导入字母、闪烁、后退按钮、闪烁处理程序
导入配置为conf
导入线程
从data.settings导入*
def net_srv():#别问我,查一查
全球sock,已连接
sock=socket.socket(socket.AF\u INET,socket.sock\u流)
袜子绑定(('',端口))
短袜。听(1);已连接=错误
类HostScreen():
定义初始化(自):
通过
def enter(自我、客户端):
conf.state=conf.state\u主机
尺寸=(宽度、高度)=[640480]
screen=pygame.display.set_模式(大小)
dt=0.0
clock=pygame.time.clock()
background=pygame.Surface(screen.get_size())
background=background.convert()
背景填充((0,0,0))
font=pygame.font.font(os.path.join(“数据”、“图形”、“lucon.ttf”),11)
text=font.render(“等待其他播放器连接。”,1,(255,255,255))
textpos=text.get_rect()
textpos.center=background.get_rect().center
textpos.centery-=10
f=字母(“f”)
l=字母(“l”)
i=字母(“i”)
c=字母(“c”)
k=字母(“k”)
e=字母(“e”)
r=字母(“r”)
闪烁=闪烁()
handler=flickethandler()
后退=后退按钮()
所有精灵2=(f,l,i,c,k,e,r)
clickables=[返回]
对于AllSprite2中的s:
s、 更新(1)
s、 rect.centery=s.rect.centery-50
allsprites=pygame.sprite.LayeredDirty(AllSprite2,闪烁,可点击)
闪烁。更新(1)
timetoFlicker=0.5
标题开关=0
net_srv()
def connect():
全球连接,地址,已连接
conn,addr=sock.accept()
已连接=真
线程=线程。线程(目标=连接)
thread.start()
而1:
dt=(时钟滴答声(60)/1000.0)
timetoFlicker-=dt
pygame.display.set_标题(“闪烁%d fps”%clock.get_fps())
events=pygame.event.get()
对于事件中的事件:
如果event.type==pygame.QUIT:sys.exit()#使X按钮工作。
如果event.type==MOUSEBUTTONDOWN:#检查是否单击。
如果event.button==1:
对于可单击项中的对象:
如果object.rect.collidepoint(pygame.mouse.get_pos())和object.clickCheck()=“返回”:
闪烁。更新(1)
timetoFlicker=0.09
标题开关=1
如果已连接:返回(conn、addr、“服务器”)
elif不是线程。是否为活动线程()
闪烁。更新(1)
timetoFlicker=0.09
标题开关=1
如果timetoFlicker 0和vis==0:
timetoFlicker=0.02
elif标题开关>0:
sock.close()
返回错误
pygame.event.pump()
如果不闪烁。可见:
screen.blit(文本,textpos)
rects=allsprites.draw(屏幕)
pygame.display.update(rects)
pygame.display.flip()

你知道是什么导致了挂起吗?

#别问我,如果你能在挂起时发布ctrl+c的回溯,可能会有助于回答问题,但我猜你的套接字会被阻塞(
socket.setblocking
)或者考虑有一个单独的通信线程运行,可以在不挂起的情况下被阻止。app@SiHa@deinonychusaur不幸的是,Ctrl-C不起作用,当Windows检测到它冻结并允许我退出时,我必须关闭它。