Last active
June 29, 2022 09:09
-
-
Save Zheaoli/108546daf349de358761f9f8b9664a53 to your computer and use it in GitHub Desktop.
Connection in Use Case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import time | |
s = socket.socket() | |
host = "127.0.0.1" | |
port = 12345 | |
s.connect((host, port)) | |
print(s.recv(1024).decode()) | |
time.sleep(10000000) | |
s.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import time | |
import os | |
def abc(): | |
s = socket.socket() | |
host = "127.0.0.1" | |
port = 12345 | |
s.bind((host, port)) | |
# print(s.error) | |
s.listen(5) | |
while True: | |
c, addr = s.accept() | |
print("连接地址:", addr) | |
c.send("Manjusaka!".encode()) | |
break | |
s.close() | |
while True: | |
abc() | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
这个问题和客户端无关, 就算客户端在收到 fin 之后及时 close, 服务端也会残留 TIME_WAIT 导致 address in use.
你的最小复现代码里, 把 client.py 里的 sleep 删掉一样可以复现, 足以证明和客户端行为无关, 只是残留状态一个是 TIME_WAIT 一个是 FIN_WAIT2, 这是 TCP 的设计.
解决这个问题要用 SO_REUSEADDR 和 SO_REUSEPORT.