Programming a TCP/IP socket in Python
Sockets provide a form of inter-process communication (IPC) used to send messages across the network. The obvious example is the Internet, which you connect to via your Internet Service Provider.
There are three main types of sockets. Here are the TCP and UDP ones:
- Stream sockets: They allow processes to communicate using Transmission Control Protocol (TCP). They provide a reliable, bidirectional, sequenced, and unduplicated flow of data. The socket type is
SOCK_STREAM
. - Datagram sockets: They allow processed to communicate using User Datagram Protocol (UDP). They support a bidirectional flow of messages, but they may receive duplicate messages or messages in a different order from the sending sequence. The socket type is
SOCK_DGRAM
.
The rest of the article focuses on the stream socket, containing a more detailed explanation and an example of how to implement it in Python.
TCP Sockets
The best way to describe the TCP/IP connections is by comparing them to a telephone call. Firstly, there is a call from the Client. Instead of having a phone number, in TCP/IP connections we have an IP Address and a port. Similar to the phone calls, the Client needs to know the IP Address and port from where he wants to send/receive the data once the connection is…