how tcp connection works?

本文介绍如何利用tcpdump工具观察Linux系统上TCP连接的建立与终止过程。通过nc和telnet工具创建TCP服务器和客户端,并使用netstat验证连接状态。文章详细展示了使用tcpdump捕获三次握手和四次挥手的数据包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This post describes how to see TCP connection establishment and termination as packets using tcpdump on linux.

Preparing
Install following commands on your linux.

  • tcpdump
  • nc
  • telnet
  • netstat

See TCP connection establishment

1. start TCP server

Start TCP server using nc command with l,k option.
$ nc -lk 12345

Open a Listening port on Linux

Open another terminal and verify 12345 port is listening using netstat command.
$ netstat -anp | grep 12345
tcp 0 0 0.0.0.0:12345 0.0.0.0:* LISTEN /nc

2. start TCP client and establish connection

Start TCP client using telnet to establish TCP connection with TCP server of step 1.
$ telnet 127.0.0.1 12345
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]’.
Open another terminal and verify nc process and telnet are establishing connection using netstat command.

5 ways to Check a remote port is open in Linux

$ netstat -anp | grep 12345
tcp 0 0 0.0.0.0:12345 0.0.0.0:* LISTEN /nc
tcp 0 0 127.0.0.1: 127.0.0.1:12345 ESTABLISHED /telnet
tcp 0 0 127.0.0.1:12345 127.0.0.1: ESTABLISHED /nc
Terminate TCP client with type “Ctrl+[” and “quit” on telnet. Then Connection is close.
$ telnet 127.0.0.1 12345
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]’.
^]
telnet> quit
Connection closed.
$
It’s ready to see TCP connection establishment with tcpdump.

3. See TCP 3-Way Handshake as TCP connection establishment

Verify TCP server that start at step 1 listen 12345 port.
$ netstat -anp | grep 12345
tcp 0 0 0.0.0.0:12345 0.0.0.0:* LISTEN /nc
Perform tcpdump with specify local interface and port 12345 as follows.
$ sudo tcpdump -i lo -nnn port 12345
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
Start TCP client using telnet to establish TCP connection with TCP server of step 1.
$ telnet 127.0.0.1 12345
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]’.

Tcpdump: Filter Packets By Port
Verify tcpdump output as follows.
HH:mm:ss.SSSSSS IP 127.0.0.1. > 127.0.0.1.12345: Flags [S], seq …
HH:mm:ss.SSSSSS IP 127.0.0.1.12345 > 127.0.0.1.: Flags [S.], seq …
HH:mm:ss.SSSSSS IP 127.0.0.1. > 127.0.0.1.12345: Flags [.], ack …
The format is as follows
timestamp IP source IP.port destination > IP.port: flags
First line means a SYN packet as “[S]” flag that telnet sent to TCP server.
Second line means SYN + ACK packet as “[S.]” flag that TCP server sent to telnet.
Third line means ACK packet as “[.]” flag that TCP server sent to telnet.
Exploring Tcpdump Filters with Examples
Understanding TCP Socket With Examples

See TCP connection termination

Open another terminal and verify nc process and telnet are establishing connection using netstat command.
$ netstat -anp | grep 12345
tcp 0 0 0.0.0.0:12345 0.0.0.0:* LISTEN /nc
tcp 0 0 127.0.0.1: 127.0.0.1:12345 ESTABLISHED /telnet
tcp 0 0 127.0.0.1:12345 127.0.0.1: ESTABLISHED /nc

3. See terminate TCP connection establishment

Keep tcpdump, and terminate TCP client with type “Ctrl+[” and “quit” on telnet. Then Connection is close.
$ telnet 127.0.0.1 12345
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]’.
^]
telnet> quit
Connection closed.
$
Verify tcpdump output as follows.

Understanding TCP Flags SYN ACK RST FIN URG PSH
HH:mm:ss.SSSSSS IP 127.0.0.1. > 127.0.0.1.12345: Flags [F.], seq 1,
HH:mm:ss.SSSSSS IP 127.0.0.1.12345 > 127.0.0.1.: Flags [F.], seq 1,
HH:mm:ss.SSSSSS IP 127.0.0.1. > 127.0.0.1.12345: Flags [.], ack 2,
First line means a FIN packet as “[F]” flag that telnet sent to TCP server.
Second line means FIN + ACK packet as “[F.]” flag that TCP server sent to telnet.
Third line means ACK packet as “[.]” flag that TCP server sent to telnet.

Tcpdump: Filter Packets with Tcp Flags
Understanding TCP Connection with Examples
Understanding TCP Sequence Number with Examples

### iQ-R Series Modbus TCP Configuration and Usage For configuring the Mitsubishi iQ-R series PLC to use Modbus TCP, several key points must be considered based on available resources[^4]. The setup involves both hardware configuration through software tools provided by Mitsubishi and programming adjustments within the PLC. #### Hardware Setup via Software Tools The initial step is setting up the network parameters using GX Works2 or other compatible engineering workstations. This includes assigning an IP address to the CPU module (such as R04CPU), which will act as either a server or client depending on application requirements. Once configured correctly, this allows communication over Ethernet networks following standard Modbus TCP protocols. #### Programming Adjustments Within the PLC Programming for Modbus TCP can involve utilizing specific function blocks designed for handling data exchange according to Modbus protocol standards. These may include commands like `MBW` (for writing multiple registers) or `MBS` (to read coils). It's important that these instructions are properly integrated into ladder logic diagrams or structured text programs written in ST language used with the iQ-R series devices. Additionally, when implementing Modbus TCP communications between different systems connected via Ethernet, ensuring proper timing settings prevents issues such as timeouts during transactions[^1]. ```python # Example Python code snippet demonstrating how one might configure basic socket connections similar to what would occur internally within a PLC program. import socket def establish_connection(ip_address='192.168.1.1', port=502): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip_address, port)) return s except Exception as e: print(f"Connection failed: {e}") raise ConnectionError("Failed to connect") connection = establish_connection() if connection: print("Successfully established Modbus TCP connection.") else: print("Could not establish Modbus TCP connection.") ``` This example illustrates establishing a simple TCP/IP connection but does not cover all aspects of actual implementation inside a real-world industrial control system where more complex configurations apply.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值