我已经编写了一个如下所示的服务器端套接字程序,def __init__(self, address='127.0.0.1', port=30104): self.ret = {} # 初始化套接字 self.address = address...
我已经编写了一个如下所示的服务器端套接字程序,
def __init__(self, address='127.0.0.1', port=30104):
self.ret = {}
# initialize socket
self.address = address
self.port = port
self.socket = socket.socket()
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(('', self.port))
self.socket.listen(1)
def daemon_service(self):
while True:
conn, address = self.socket.accept()
logger.debug("starting thread for {}, {}".format(conn, address))
thread = threading.Thread(target=self.handle_client, args=[conn, address])
thread.daemon = True
thread.start()
def handle_client(self, connection, address):
try:
logger.debug("Connected {} at {}".format(connection, address))
connection.settimeout(10.0) # 10
while True:
logger.debug("Waiting to receive data from {}".format(address))
try:
data = connection.recv(40960)
except socket.timeout:
logger.debug("Connection timed out")
break
... ...
except Exception as e:
logger.exception("Problem handling request: {}".format(e))
finally:
logger.debug("Closing socket")
connection.close()
问题在启动时运行顺利,但过了一段时间,它总是卡在 connction.recv 并返回 connect timed out 一段时间,我使用 strace 调试系统并返回,我可以知道哪里出了问题以及我应该如何调试
{sa_family=AF_INET, sin_port=htons(53853), sin_addr=inet_addr("11.36.17.123")}, [16], SOCK_CLOEXEC) = 179
getsockname(179, {sa_family=AF_INET, sin_port=htons(30104), sin_addr=inet_addr("10.101.30.171")}, [16]) = 0
getsockname(179, {sa_family=AF_INET, sin_port=htons(30104), sin_addr=inet_addr("10.101.30.171")}, [16]) = 0
getpeername(179, {sa_family=AF_INET, sin_port=htons(53853), sin_addr=inet_addr("11.36.17.123")}, [16]) = 0
write(6, "2024-08-12 23:34:58,092 - root -"..., 240) = 240
write(1, "2024-08-12 23:34:58,092 - root -"..., 240) = 240
clone(child_stack=0x7f55d3b26fb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f55d3b279d0, tls=0x7f55d3b27700, child_tidptr=0x7f55d3b279d0) = 200708
futex(0x98fe24, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0x98fe20, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 1
futex(0x246b670, FUTEX_WAIT_PRIVATE, 0, NULL) = 0
futex(0x98fe24, FUTEX_WAIT_BITSET_PRIVATE, 21671, {416274, 962178869}, ffffffff) = 0
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x98fe24, FUTEX_WAIT_BITSET_PRIVATE, 21673, {416274, 962239750}, ffffffff) = 0
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x98fe20, FUTEX_WAKE_PRIVATE, 1) = 1
futex(0x98fe24, FUTEX_WAIT_BITSET_PRIVATE, 21675, {416274, 962370886}, ffffffff) = -1 EAGAIN (Resource temporarily unavailable)
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x98fe20, FUTEX_WAKE_PRIVATE, 1) = 1
futex(0x98fe24, FUTEX_WAIT_BITSET_PRIVATE, 21677, {416274, 962471485}, ffffffff) = -1 EAGAIN (Resource temporarily unavailable)
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x98fe24, FUTEX_WAIT_BITSET_PRIVATE, 21679, {416274, 962541564}, ffffffff) = -1 EAGAIN (Resource temporarily unavailable)
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x98fe24, FUTEX_WAIT_BITSET_PRIVATE, 21681, {416274, 962588861}, ffffffff) = 0
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x98fe24, FUTEX_WAIT_BITSET_PRIVATE, 21683, {416274, 962646446}, ffffffff) = -1 EAGAIN (Resource temporarily unavailable)
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x98fe24, FUTEX_WAIT_BITSET_PRIVATE, 21685, {416274, 962698958}, ffffffff) = 0
futex(0x98fe24, FUTEX_WAIT_BITSET_PRIVATE, 21687, {416274, 962698958}, ffffffff) = 0
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x98fe24, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0x98fe20, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 1
accept4(3, {sa_family=AF_INET, sin_port=htons(10830), sin_addr=inet_addr("10.205.249.254")}, [16], SOCK_CLOEXEC) = 180
getsockname(180, {sa_family=AF_INET, sin_port=htons(30104), sin_addr=inet_addr("10.101.30.171")}, [16]) = 0
getsockname(180, {sa_family=AF_INET, sin_port=htons(30104), sin_addr=inet_addr("10.101.30.171")}, [16]) = 0
getpeername(180, {sa_family=AF_INET, sin_port=htons(10830), sin_addr=inet_addr("10.205.249.254")}, [16]) = 0
write(6, "2024-08-12 23:35:26,728 - root -"..., 244) = 244
write(1, "2024-08-12 23:35:26,728 - root -"..., 244) = 244
clone(child_stack=0x7f55d3b26fb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f55d3b279d0, tls=0x7f55d3b27700, child_tidptr=0x7f55d3b279d0) = 200924
futex(0x98fe24, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0x98fe20, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 1
futex(0x2421fa0, FUTEX_WAIT_PRIVATE, 0, NULL) = 0
futex(0x98fe24, FUTEX_WAIT_BITSET_PRIVATE, 21693, {416303, 597912389}, ffffffff) = 0
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x98fe20, FUTEX_WAKE_PRIVATE, 1) = 1
futex(0x98fe24, FUTEX_WAIT_BITSET_PRIVATE, 21695, {416303, 597965191}, ffffffff) = -1 EAGAIN (Resource temporarily unavailable)
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x98fe24, FUTEX_WAIT_BITSET_PRIVATE, 21697, {416303, 598050813}, ffffffff) = 0
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x98fe24, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0x98fe20, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
futex(0x98fe50, FUTEX_WAKE_PRIVATE, 1) = 1
accept4(3,
调试为什么connection.rev返回连接超时