---
title: "TCP Handshake (part-2)"
description: "Next, let's analyze the handshake. To do this, we will need Wireshark (to listen to TCP packets and communication). Now, let's run the server and observe what happens through Wireshark’s interface."
canonical_url: "https://otabek.io/blogs/tcp-handshake-part-1"
md_url: "https://otabek.io/blogs/tcp-handshake-part-1.md"
language: "en"
last_updated: "2024-03-07"
tags: ["Networking"]
---

# TCP Handshake (part-2)

A protocol is a set of standard rules that ensure communication between two or more devices. Therefore, TCP is a protocol, and we can say that it is a standard that facilitates communication between devices.

You may have heard about the 3-way handshake (handshaking) required to establish a TCP connection. It might look something like this:

![](https://telegra.ph/file/c6ff988922c78faec880d.png)

Let’s break it down in simple terms. As usual, the conversation begins with the Client, and the Server responds. First, the Client sends a SYN flag. You may not immediately understand what’s happening, so let’s see it through code and then analyze it:

```python
import socket


def main():
    sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sck.bind(('localhost', 1712))
    sck.listen(1)

    print("I am listening on port 1712")

    (client_socket, address) = sck.accept()

    while True:
        msg = client_socket.recv(1024)
        if not msg:
            break
        print(f"FROM: {address} \nMESSAGE: {msg}")
        print()
    client_socket.close()


if __name__ == "__main__":
    main()
```

We’ve written the server side. Now, let’s look at the client side. The server is the listener, and the client is the speaker. Remember the rule: The ear doesn’t hear until the mouth speaks. :)

```python
import socket


def main():
    sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sck.connect(('localhost', 1712))
    print("Sending data...")
    sck.sendall(b"Hello server!")
    sck.close()


if __name__ == "__main__":
    main()
```

The code I’ve written is a bit sparse on comments, but writing comments was just an excuse. By adding comments, I’m explaining this code to you now.  
![](https://telegra.ph/file/f64137c0f85af3e9dea38.png)  
_The code I wrote_

Now, when we run the server first, followed by the client through the terminal, the server will keep listening. If the client tries to speak but the server doesn’t listen, the client might be frustrated.

![](https://telegra.ph/file/4920cbd174828137510ef.png)  
_The message I received_

To fully track these events, we'll need Wireshark (you might have heard this term more in network hacking—wow!). It should be available for both Windows and Mac. In the next post, we'll continue with this topic...

---

```quiz
{
  "quiz": {
    "id": "tcp-basics-quiz",
    "title": "TCP Basics Quiz",
    "description": "Test your understanding of TCP fundamentals",
    "questions": [
      {
        "id": "q1",
        "type": "single-choice",
        "question": "What is TCP?",
        "options": [
          { "id": "a", "text": "A protocol that ensures communication between devices", "description": "" },
          { "id": "b", "text": "A programming language", "description": "TCP is a protocol, not a programming language." },
          { "id": "c", "text": "A type of database", "description": "TCP is a network protocol, not a database." },
          { "id": "d", "text": "A web browser", "description": "TCP is a protocol that browsers use, not a browser itself." }
        ]
      },
      {
        "id": "q2",
        "type": "single-choice",
        "question": "In socket programming, what does SOCK_STREAM indicate?",
        "options": [
          { "id": "a", "text": "UDP connection", "description": "SOCK_STREAM is for TCP, not UDP. UDP uses SOCK_DGRAM." },
          { "id": "b", "text": "TCP connection", "description": "" },
          { "id": "c", "text": "File streaming", "description": "SOCK_STREAM refers to TCP stream sockets, not file streaming." },
          { "id": "d", "text": "Video streaming", "description": "SOCK_STREAM is a socket type for TCP, not video streaming." }
        ]
      },
      {
        "id": "q3",
        "type": "drag-fill",
        "question": "Complete the server socket setup:",
        "template": "sck = socket.socket(socket.AF_INET, socket.{{b1}})\\nsck.{{b2}}(('localhost', 1712))\\nsck.{{b3}}(1)",
        "options": [
          { "id": "opt1", "content": "SOCK_STREAM" },
          { "id": "opt2", "content": "bind" },
          { "id": "opt3", "content": "listen" }
        ],
        "blanks": [
          { "id": "b1" },
          { "id": "b2" },
          { "id": "b3" }
        ]
      },
      {
        "id": "q4",
        "type": "single-choice",
        "question": "Which side initiates the TCP connection?",
        "options": [
          { "id": "a", "text": "The server", "description": "The server listens, the client initiates the connection." },
          { "id": "b", "text": "The client", "description": "" },
          { "id": "c", "text": "Both at the same time", "description": "The client initiates by sending the first SYN packet." },
          { "id": "d", "text": "Neither - it's automatic", "description": "The client must explicitly connect to the server." }
        ]
      },
      {
        "id": "q5",
        "type": "drag-drop",
        "question": "Arrange the server-side socket operations in correct order:",
        "items": [
          { "id": "create", "content": "Create socket" },
          { "id": "bind", "content": "Bind to address and port" },
          { "id": "listen", "content": "Listen for connections" },
          { "id": "accept", "content": "Accept client connection" },
          { "id": "recv", "content": "Receive data" }
        ]
      },
      {
        "id": "q6",
        "type": "single-choice",
        "question": "What is the first flag sent in a TCP handshake?",
        "options": [
          { "id": "a", "text": "ACK", "description": "ACK comes later. The first flag is SYN." },
          { "id": "b", "text": "SYN", "description": "" },
          { "id": "c", "text": "FIN", "description": "FIN is used to close connections, not start them." },
          { "id": "d", "text": "RST", "description": "RST is for resetting connections, not initiating them." }
        ]
      }
    ]
  },
  "answers": {
    "q1": { "correctOptionIds": ["a"] },
    "q2": { "correctOptionIds": ["b"] },
    "q3": { "correctPlacements": { "b1": "opt1", "b2": "opt2", "b3": "opt3" } },
    "q4": { "correctOptionIds": ["b"] },
    "q5": { "correctOrder": ["create", "bind", "listen", "accept", "recv"] },
    "q6": { "correctOptionIds": ["b"] }
  }
}
```


## Sitemap

See the full [Markdown sitemap](/sitemap.md) for all pages.
