问题描述
我正在尝试使用套接字,tkinter等制作一个邮件应用程序.启动该程序时,它在加载屏幕后冻结.它没有显示任何错误,只是冻结了.我调试了它,然后找出错误"在哪里.我尝试使用 setblocking()
或 settimeout()
等,但是没有任何效果.也许是tkinter的问题?这是我的客户端脚本和服务器脚本中最重要的部分
I'm trying to make a mail app, using socket, tkinter, etc. When I launch the program, it freezes after the loading screen. It doesn't show any error, just freezes. I debugged it, and found out where the 'error' was. I've tried using setblocking()
or settimeout()
, etc, but nothing worked. Maybe tkinter is the problem? Here are the most important parts of my client script, and server script
客户
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
[...]
def after():
client_socket.connect((IP, PORT))
while True:
msg = client_socket.recv(HEADER) # It freezes here (I found out it was here by debugging)
try:
msg = msg.decode(FORMAT)
except Exception:
msg = pickle.loads(msg)
if msg:
pass
def load_win():
loading_bar.pack_forget()
loading_title.pack_forget()
loading_frame.pack_forget()
main_label.pack()
log_in_button.pack(pady=30)
sign_in_button.pack()
main_frame.pack(expand=YES)
win.geometry('720x480')
win.minsize(720, 480)
win.config(bg='#3D3D3D')
win.after(0, after)
def load_bar():
loading_bar['value'] += loading_bar['length'] // 100
def load():
total_nbr = 0
for x in range(100):
randint = random.randint(1000, 5000)
total_nbr += randint
loading_bar.after(randint, load_bar)
win.after(total_nbr//100, load_win)
[...]
win.after(0, load)
win.mainloop()
服务器
[...]
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((IP, PORT))
server_socket.listen()
[...]
while True:
client_socket, client_address = server_socket.accept()
[...]
while True:
msg = None
try:
msg = client_socket.recv(HEADER)
except Exception:
break
try:
msg = msg.decode(FORMAT)
except Exception:
msg = pickle.loads(msg)
if msg == 'get_clients':
client_socket.send(pickle.dumps(clients))
推荐答案
也许是因为在 msg = client_socket.recv(HEADER)
中,方法 recv()
是阻止代码,代码会暂停,直到 recv()
收到消息,然后转到下一行代码.
Maybe it is because in msg = client_socket.recv(HEADER)
the method recv()
is a blocking code and the code is paused until recv()
recieves a message and then goes to next lines of code.
这篇关于当我在客户端套接字上使用'recv()'时,我的python程序死机了,我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!