Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Transmission Control Protocol (TCP) Sockets

00:00 In this lesson, you’ll see how sockets set up and maintain connections between the server and client programs. There are two Internet protocols sockets can use when sending data.

00:11 The first is Transmission Control Protocol, TCP. Its important properties include that it’s reliable, meaning packets lost during transmission can be detected by the sender and resent, and the data is read by the receiver in the order it was sent, called in-order data delivery. The other protocol is User Datagram Protocol.

00:34 It uses less overhead since it doesn’t track for lost packets or similar issues, so it’s primarily used where speed of transmission is more important than reliability. Of these, TCP is the default protocol sockets use, and it’s the most frequently one used, mainly because it does try to react to lost packets.

00:59 Here’s a diagram showing the activities in the server and client processes and the timing of each relative to the other as data is sent and received. The server begins by creating a socket, binding it to a port, listening for client requests, and finally accepting one when one comes in.

01:18 That request happens when the client process creates a socket and requests a connection to the server. Then the client sends data indicating what it’s wanting from the server, which the server then receives.

01:32 The server then processes that incoming data with the request, performs the actions needed to return the data desired, then sends that to the client process, which then receives the data and uses it however it was intended.

01:47 Then the client indicates it’s closing the connection, which the server receives and then closes its side of the connection.

01:57 This can be broken down into four phases. First, the server creates its listening socket. It does this with the creation of a socket, then using the .bind(), .listen(), and then, when requested, .accept() method calls.

02:13 For its part, the client creates its own socket, then calls the .connect() method to initiate a connection with the server. Then the two processes alternate sending and receiving data, and finally the connection is closed.

02:31 In the next few lessons, you’ll see how each of those method calls are used in the creation of a simple echo server.

Become a Member to join the conversation.