我正在客户端服务器程序下面运行,该程序通过本地环路地址进行通信,并尝试捕获 tcpdump 输出。虽然程序专门设置了定期发送保活消息,但我在 tcpdump 中没有看到它。知道为什么吗?
服务器.py
import socket
def start_server():
server_host = '127.0.0.1'
server_port = 12345
backlog = 5 # Maximum number of queued connections
# Create a TCP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Enable TCP keep-alive on the server socket
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
# Set TCP keep-alive options (optional, you can adjust these as needed)
server_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 60)
server_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 10)
server_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
# Bind the socket to the server address and port
server_socket.bind((server_host, server_port))
# Listen for incoming connections
server_socket.listen(backlog)
print(f"Server is listening on {server_host}:{server_port}")
while True:
print("Waiting for a connection...")
client_socket, client_address = server_socket.accept()
print(f"Accepted connection from {client_address}")
while True:
data = client_socket.recv(1024)
if not data:
break
print(f"Received: {data.decode('utf-8')}")
client_socket.send(data) # Echo the data back to the client
client_socket.close()
print(f"Connection with {client_address} closed")
if __name__ == "__main__":
start_server()
客户端.py
import socket
import time
def start_client():
server_host = '127.0.0.1'
server_port = 12345
# Create a TCP socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Enable TCP keep-alive on the client socket
client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
# Set TCP keep-alive options (optional, you can adjust these as needed)
client_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 60)
client_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 10)
client_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
try:
# Connect to the server
client_socket.connect((server_host, server_port))
print(f"Connected to {server_host}:{server_port}")
while True:
message = input("Enter a message to send (or 'exit' to quit): ")
if message == 'exit':
break
client_socket.send(message.encode('utf-8'))
response = client_socket.recv(1024)
print(f"Server response: {response.decode('utf-8')}")
finally:
client_socket.close()
print("Connection closed")
if __name__ == "__main__":
start_client()
我保持套接字通道打开 5 分钟,没有任何活动,但没有看到任何保持活动的 pkt,但是仍然有一些 pkt 传输定期发生,但似乎没有保持活动
/home/ravi> tcpdump -i lo -n -vvv 'port 12345'
tcpdump: listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
....
....
03:41:48.073820 IP (tos 0x0, ttl 64, id 50758, offset 0, flags [DF], proto TCP (6), length 52)
127.0.0.1.12345 > 127.0.0.1.49088: Flags [.], cksum 0xfe28 (incorrect -> 0x38ba), seq 4, ack 4, win 512, options [nop,nop,TS val 2055232093 ecr 2055170654], length 0
03:41:48.073832 IP (tos 0x0, ttl 64, id 27799, offset 0, flags [DF], proto TCP (6), length 52)
127.0.0.1.49088 > 127.0.0.1.12345: Flags [.], cksum 0xfe28 (incorrect -> 0x38ba), seq 3, ack 5, win 512, options [nop,nop,TS val 2055232093 ecr 2055170654], length 0
03:41:48.073868 IP (tos 0x0, ttl 64, id 27800, offset 0, flags [DF], proto TCP (6), length 52)
127.0.0.1.49088 > 127.0.0.1.12345: Flags [.], cksum 0xfe28 (incorrect -> 0x38b9), seq 4, ack 5, win 512, options [nop,nop,TS val 2055232093 ecr 2055170654], length 0
03:41:48.073876 IP (tos 0x0, ttl 64, id 50759, offset 0, flags [DF], proto TCP (6), length 52)
127.0.0.1.12345 > 127.0.0.1.49088: Flags [.], cksum 0xfe28 (incorrect -> 0x38b9), seq 5, ack 4, win 512, options [nop,nop,TS val 2055232093 ecr 2055170654], length 0
....
....
进展顺利,但我们这里有一个小问题。 tcpdump 很容易记录应用程序级别的所有流量。
KEEP_ALIVE 是内核级配置。你通常做的是设置套接字选项,正如我在你的代码中看到的那样。发布此内容后,保持连接活动的责任是操作系统(具体来说是内核)之间发生的交互,因此在应用程序级别(因此转储)我们将不会看到任何数据包经过。
希望这有帮助。