我正在用 python 进行套接字编程,我想在服务器端检测客户端套接字断开连接。
Client:
socket.connect(host, port)
try:
""" send something to server and get response from the server"""
except KeyboardInterrupt:
socket.shutdown(socket.SHUT_RDWR)
socket.close()
Server:
conn, address = socket.accept()
try:
conn.send(message)
except:
print "-1"
但是当我第一次运行这段代码时,我通过键盘中断关闭客户端,服务器不会在屏幕上打印-1。当我再次尝试时,服务器将捕获异常并打印-1。
我想知道我的代码是否有问题。为什么服务器不能第一时间检测到断线?
Thanks.
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)