Understanding TCP, UDP, and the Three-Way Handshake in Networking

2025-02-06

Networking Refresher Note:

TCP vs. UDP:

  • TCP (Transmission Control Protocol):
    TCP is a reliable, connection-oriented protocol. It ensures that data is delivered correctly and in the correct order. This makes it ideal for applications where data integrity is crucial, such as web browsing, email, and file transfers. TCP achieves this reliability through mechanisms like error checking, retransmission of lost packets, and flow control.

  • UDP (User Datagram Protocol):
    UDP is a fast, connectionless protocol that prioritizes speed over reliability. It does not guarantee the delivery of packets, nor does it ensure they arrive in order. This makes UDP suitable for real-time applications like video streaming, online gaming, VoIP (Voice over IP), and DNS lookups, where speed is more critical than perfect data delivery.

The Three-Way Handshake:

The Three-Way Handshake is a fundamental process in TCP that establishes a connection between a client and a server. It involves three steps:

1. SYN (Synchronize):

The client sends a SYN packet to the server to initiate a connection. This packet contains a random sequence number that the server will use to acknowledge the request.

2. SYN-ACK (Synchronize-Acknowledge):

The server responds with a SYN-ACK packet. This packet acknowledges the client’s SYN request and also includes the server’s own SYN request, along with a different sequence number.

3. ACK (Acknowledge):

The client sends an ACK packet back to the server, confirming the connection. Once this packet is received, the connection is established, and data transfer can begin.

Common Ports and Protocols:

  • FTP (File Transfer Protocol): Port 21, TCP
  • SSH (Secure Shell): Port 22, TCP
  • DNS (Domain Name System): Port 53, TCP/UDP
  • HTTP/HTTPS (Hypertext Transfer Protocol/Secure): Ports 80/443, TCP
  • DHCP (Dynamic Host Configuration Protocol): Ports 67/68, UDP
  • VoIP, Streaming, Gaming: UDP

Practical Commands and Codes:

1. TCP Connection Test with `nc` (Netcat):

To test a TCP connection to a specific port, you can use the `nc` command:

nc -vz example.com 80

This command checks if port 80 on `example.com` is open and accepting TCP connections.

2. UDP Connection Test with `nc`:

To test a UDP connection, use the `-u` flag:

nc -vzu example.com 53

This checks if port 53 (commonly used for DNS) is open for UDP traffic.

3. Viewing Open TCP/UDP Ports with `netstat`:

To see all open TCP and UDP ports on your system, use:

netstat -tuln

This command lists all listening ports and the protocols they are using.

4. Capturing TCP Handshake with `tcpdump`:

To capture the Three-Way Handshake in action, use:

tcpdump -i eth0 'tcp[tcpflags] & (tcp-syn|tcp-ack) != 0'

This captures SYN and ACK packets on the `eth0` interface, allowing you to observe the handshake process.

5. Testing HTTP/HTTPS Connectivity with `curl`:

To test if a web server is reachable over HTTP or HTTPS, use:

curl -I http://example.com
curl -I https://example.com

These commands fetch the headers of the HTTP/HTTPS response, confirming connectivity.

What Undercode Say:

Understanding the differences between TCP and UDP, as well as the Three-Way Handshake, is crucial for anyone involved in networking, cybersecurity, or IT. These concepts form the backbone of how data is transmitted over the internet, and mastering them can help you troubleshoot network issues, optimize performance, and secure your systems.

In Linux, tools like netstat, nc, and `tcpdump` are invaluable for diagnosing and analyzing network traffic. For example, `netstat` can help you identify open ports and active connections, while `tcpdump` allows you to capture and inspect packets in real-time. These tools, combined with a solid understanding of TCP and UDP, can give you deep insights into your network’s behavior.

For further reading, consider exploring the following resources:

By mastering these concepts and tools, you’ll be well-equipped to handle a wide range of networking challenges, from troubleshooting connectivity issues to securing your systems against potential threats. Whether you’re a beginner or an experienced professional, continuous learning and practice are key to staying ahead in the ever-evolving field of networking and cybersecurity.

References:

Hackers Feeds, Undercode AIFeatured Image

Scroll to Top