Course contentsShow
← Computer Science
Lesson 1544 of 2,87235. Application Layer ProtocolsFree lesson

TCP Client: Connect to Server

Understand how TCP clients initiate connections to servers using the connect operation.

TCP Client: Connect to Server

What You'll Learn

You'll learn how a TCP client initiates a connection to a server using the connect operation.


The Client's Role in Connection Setup

While the TCP server sits waiting with listen and accept, the client takes the active role by initiating the connection. The client uses the connect operation to reach out to a specific server at a known IP address and port number.

Think of it like making a phone call: the server is listening with the phone ready, and the client dials the number to start the conversation. The connect call is the client's "dial" action.

How Connect Works

When a client calls connect on its socket, it provides two key pieces of information:

  1. Server's IP address – where the server is located on the network
  2. Server's port number – which application on that server to reach

Behind the scenes, connect triggers the TCP three-way handshake you learned earlier (SYN → SYN-ACK → ACK). The client sends the initial SYN packet, waits for the server's SYN-ACK, and completes the handshake with an ACK.

The connect call is blocking by default—it won't return until the handshake completes successfully or fails with a timeout or error.

When Connect Completes

Once connect returns successfully, the socket is now in an ESTABLISHED state. The client can immediately begin sending and receiving data over this connection. The hard work of connection setup is done.

If the server isn't reachable, the port is closed, or a firewall blocks traffic, connect will fail and return an error.


Key Takeaway: The connect operation allows a TCP client to actively initiate a connection to a server by specifying the server's address and port, triggering the three-way handshake and establishing a bidirectional communication channel.