Example: Use Sockets to Create a TCP Connection

The following steps describe a TCP connection process using sockets.

  1. Server Creates Socket and Listens
  2. Client Creates a Socket and Connects
  3. Transport Layer Delivers Message to Server
  4. Server Creates Socket & Process
  5. Transport Layer Delivers Message to Client
  6. Sockets Closed

1 Server Creates Socket and Listens

A web server creates a socket dedicated to listening for client requests. After the socket exists, the server goes into “listening” mode and waits for a client's request. It periodically checks for messages received in this socket.

This type of socket is referred to as a connectionless socket. A connectionless socket is used to establish a TCP connection with the HTTP server. There is no destination IP address or port number defined for this type of socket.

socket_step_1.JPG
Click image to enlarge.

2 Client Creates a Socket and Connects

When a client wants to download a web page it creates a socket then sends the web page download request to the socket.

socket_step_2.JPG
Click image to enlarge.

3 Transport Layer Delivers Message to Server

The client’s Transport layer periodically checks its transmit buffers to determine if a message needs to be sent. When a message is found it is forwarded to the destination address.

socket_step_3.JPG

4 Server Creates Socket & Process

When the server receives the client’s request, it creates a new dedicated socket and process. It then creates a message for the client and sends it to the socket.

Note this socket uses the client’s destination IP address and port number. This virtual TCP connection is now referred to as “established”.

socket_step_4.JPG

The message sent by the server is the HTML file for the requested webpage.

5 Transport Layer Delivers Message to Client

The server’s Transport layer periodically checks its transmit buffers to determine if a message needs to be sent.

When a message is found it is forwarded to the destination address.

socket_step_5.JPG

6 Sockets Closed

After the client receives the web page it requested, it sends an acknowledge to the server then closes its socket.

The server receives the client’s acknowledge then closes its socket.

socket_step_6.JPG

20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.