ํ์ ๋ ์์ port๋ฅผ ์ฌ์ฉํ ์ ์๋ client์์ ์ผ์ ์๊ฐ์ด ์ง๋ ํ์ ๋ ์ด์ ํต์ ์ ํ์ง ์๋ ํ์์ด ๋ฐ๊ฒฌ๋จ.
# netstat -ano | find "<server_port>" (windows)
>netstat -ano | find "11111"
ํ๋กํ ์ฝ ๋ก์ปฌ ์ฃผ์ ์ธ๋ถ ์ฃผ์ ์ํ
TCP 127.0.0.1:11111 127.0.0.1:53481 FIN_WAIT_2 28360
TCP 127.0.0.1:53481 127.0.0.1:11111 CLOSE_WAIT 6160
ํด์ค: Client๋ CLOSE_WAIT์ํ๋ค.
TCP/IP ํต์ ์์ LOSE_WAIT ๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ๋ค.
TCP/IP ํต์ ์ ์ข
๋ฃ ์์ ์ ๋ peer๋ ๊ฐ๊ฐ active closer์ passive closer๋ก ๋๋๋ค. active closer๋ FIN์ ๋จผ์ ๋ณด๋ด๋ ์ชฝ์ด๊ณ , passive closer๋ FIN์ ๋ฐ๊ณ , ACK๋ฅผ ๋ณด๋ด๋ ์ชฝ์ด๋ค.

์ฆ, client๊ฐ LOSE_WAIT์ํ์์ ๋๊ธฐ ์ค์ด๋ผ๋ ๊ฒ์ server๊ฐ tcp ํต์ ์ ๋จผ์ ์ข
๋ฃ(FIN์ ๋ณด๋)ํ๊ณ , client์์ ๊ทธ ์์ฒญ์ ๋ํด ACK๋ฅผ ์ ๋ฌํ์์ผ๋, ์ดํ FIN์ ์ ๋ฌํ์ง ์์๋ค๋ ๋ป์ด๋ค.
CLOSE_WAIT์ ์์๋ก ์ฌํํ๋ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
# server
import socket
def start_server():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('127.0.0.1', 11111))
server.listen(5)
print("Server started. Waiting for connections...")
conn, addr = server.accept()
print(f"Connection from {addr} established!")
data = conn.recv(1024)
print("Received:", data.decode())
conn.shutdown(socket.SHUT_RDWR) # Send FIN
conn.close()
print("Server connection closed")
if __name__ == "__main__":
start_server()
server๋ recv() ์ดํ ์ฐ๊ฒฐ์ ๋๋๋ค.
# client
import socket
import time
def start_client():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 11111))
client.sendall(b'Hello, Server!')
time.sleep(10)
client.close()
if __name__ == "__main__":
start_client()
client๋ sendall() ์ดํ sleep()ํ์ฌ process๋ฅผ block๋ ์ํ๋ก ๋๋ค.

wireshark - No. 24 ์ดํ CLOSE_WAIT์์ ๋๊ธฐํ๋ client๊ฐ NO. 34์์ FIN์ ๋ณด๋ด๊ณ TCP/IP ํต์ ์ ์ข ๋ฃ
No. 34๊น์ง 10์ด ๊ฐ๋์ ๋๋ ์ด๊ฐ ์๋๋ฐ, ์ด๋ ํ ์คํธ ์ฝ๋์ client๊ฐ 10์ด ๋์ sleepํ๊ธฐ ๋๋ฌธ์ด๋ค.
>netstat -ano | find "11111"
ํ๋กํ ์ฝ ๋ก์ปฌ ์ฃผ์ ์ธ๋ถ ์ฃผ์ ์ํ
TCP 127.0.0.1:11111 127.0.0.1:53481 FIN_WAIT_2 28360
TCP 127.0.0.1:53481 127.0.0.1:11111 CLOSE_WAIT 6160
>netstat -ano | find "11111"
ํ๋กํ ์ฝ ๋ก์ปฌ ์ฃผ์ ์ธ๋ถ ์ฃผ์ ์ํ
TCP 127.0.0.1:11111 127.0.0.1:53481 TIME_WAIT 0
senall()์ ํธ์ถํ ํ, sleep()์ ํธ์ถํ๋ค. ๊ทธ ์ํ์์ server๋ shutdown()์ ์ํด active closer๋ก์ FIN์ ๋ณด๋ธ๋ค.socket.recv() returns EOF), ์ด๋ฅผ ํ์ธํ client๊ฐ socket.close()๋ฅผ ํธ์ถํ๊ธฐ๋ฅผ ๊ธฐ๋ค๋ ค FIN์ ๋ณด๋ธ๋ค.sleep()์ ํธ์ถํ๊ณ 10์ด ํ์ socket.close()๋ฅผ ํธ์ถํ๋ค. ๋ฐ๋ผ์ ์ด ์ฌ์ด์ ํด๋น socket์ CLOSE_WAIT ์ํ๋ก ๋๊ธฐํ๋ค.CLOSE_WAIT์ ๋ฌธ์ ๋ timeout์ด ์๋ค๋ ์ ์ด๋ค. ์ ์์์์ active closer์ FIN์ ๋ํ passive closer์ ACK๋ฅผ ๋ฐ์ active closer๋ FIN_WAIT2 ์ํ๊ฐ ๋๋ค. FIN_WAIT2๋ linux์์ tcp_fin_timeout์ผ๋ก timeout์ด ์ ํด์ ธ ์์ด server์์ ํด๋น ๋ฌธ์ ๊ฐ ๋ฐ๋ณต๋๋๋ผ๋ port๊ฐ ๊ณ ๊ฐ๋๋ ์ผ์ ๋ฐ์ํ์ง ์๋๋ค. ํ์ง๋ง CLOSE_WAIT์ timeout์ด ์์ด ๊ทธ ์ํ๋ก ๋ฌดํํ ๋๊ธฐํ๋ค.