CPNT217 - 16. Socket Programming
CPNT217 - 16. Socket Programming
Programming
Introduction to socket programming
What is Socket
programming
Creating a basic socket
Why use sockets?
Socket Connection
The server receives the request on port 80 and sends a reply using port 80 as the new source. Note the reversal
of both the IP addresses and port numbers between the request and the reply.
IP Address Port
Source 112.156.54.89 9835
Destination 172.20.52.100 80
Internet
IP Address Port
Source 172.20.52.100 80
Destination 112.156.54.89 9835
Socket Programming – Background
Socket programming is a part of creating network-enabled applications that allow
different devices to communicate with each other over a network. In this type of
programming, developers use sockets to establish connections and transfer data
between devices.
A socket is a software abstraction that represents an endpoint of a network
connection. Each socket is associated with a unique IP address and port number,
which are used to identify the device and the specific application that is sending or
receiving data.
Socket programming involves two types of sockets: client sockets and server
sockets. Client sockets are used to initiate a connection with a server socket, while
server sockets listen for incoming connections from client sockets.
Socket Programming – Procedure
The process of socket programming involves the following steps:
• Creating a socket: In this step, developers create a socket object and specify
the protocol family, socket type, and protocol.
• Binding the socket: In this step, developers bind the socket to a specific IP
address and port number.
• Listening for incoming connections (for server sockets): Server sockets
enter a listening state and wait for incoming connections from client sockets.
• Accepting connections (for server sockets): When a client socket connects to
the server socket, the server socket accepts the connection and creates a new
socket object to handle the connection.
• Sending and receiving data: Once the connection is established, both the
client and server sockets can send and receive data through the socket.
• Closing the connection: When the communication is complete, the sockets
are closed to release the resources used by the connection.
More Socket Programming
In both cases, the first argument to the socket() function specifies the protocol family (in this case,
AF_INET for IPv4) and the second argument specifies the socket type (in this case, SOCK_STREAM for
TCP and SOCK_DGRAM for UDP).
Setting up a Socket
Server
Create a socket server
You can bind a socket server to an IP address and port using the bind()
method of the socket object. The bind() method takes a tuple
argument that specifies the IP address and port number to bind to.
The IP address can be a string representing a hostname or an IP
address in dotted-quad notation.
Moving Data
• To send and receive data with
sockets in Python, you can
use the send() and recv()
methods of the socket object.
The send() method is used to
send data over the socket,
while the recv() method is
used to receive data from the
socket.
Secure Socket Layer (SSL)
• SSL (Secure Sockets Layer) is a protocol that provides secure communication
over a network. It is commonly used for securing web traffic (HTTPS) but can
also be used for other types of network communication, including socket
programming.
• Some advantages of using SSL include:
• Encryption: SSL uses encryption to protect the confidentiality of data transmitted
over a network, making it difficult for attackers to intercept and read the data.
• Authentication: SSL provides mechanisms for verifying the identities of the
communicating parties, helping to prevent impersonation and man-in-the-middle
attacks.
• Integrity: SSL includes mechanisms for detecting and preventing tampering with
the data being transmitted over the network, ensuring its integrity.
• To create an SSL socket in Python, you can use the ssl module in combination
with the socket module
Note: SSL has been superseded by Transport Layer Security (TLS), which offers better encryption and more security features.
WebSockets
• WebSockets is a method for bidirectional communication between a
client and a server over a network. Unlike regular HTTP connections,
which are request-response based, WebSockets enables ongoing, real-
time communication between the client and server.
Here are some additional resources for learning socket programming with Python:
• Python documentation on socket programming: https://docs.python.org/3/library/socket.html
• Real Python's Socket Programming in Python (Guide): https://realpython.com/python-sockets/
• Socket Programming HOWTO (Python 3): https://docs.python.org/3/howto/sockets.html
• Socket Programming in Python (GeeksforGeeks): https://www.geeksforgeeks.org/socket-
programming-python/
• Introduction to Network Programming with Python (Udacity):
https://www.udacity.com/course/introduction-to-networking--ud436