| title | Unit 2: Socket Programming: Creating Network Applications | ||||||
|---|---|---|---|---|---|---|---|
| id | unit2-topic8 | ||||||
| tags |
|
||||||
| aliases |
|
We have studied the principles of application-layer protocols, but how are they implemented? The answer lies in socket programming.
[!note] Definition: A socket is a software interface—an Application Programming Interface (API)—that provides a "doorway" for an application process to send and receive messages to/from the network.
When creating a socket, the developer must choose a transport protocol, which determines the type of socket created.
TCP is connection-oriented. A connection must be established before data can be sent.
socket(): Create a "listening socket".bind(): Assign a port number to the socket.listen(): Tell the socket to listen for incoming connection requests.accept(): Wait for a client to connect. When one does, create a new, dedicated connection socket for that client.recv()/send(): Communicate with the client using this new socket.close(): Close the connection socket.
socket(): Create a client socket.connect(): Initiate a TCP connection to the server.send()/recv(): Exchange data.close(): Close the connection.
UDP is connectionless. There is no handshake and no dedicated connection.
socket(): Create a UDP socket.bind(): Assign a port number to it.recvfrom(): Wait for a datagram to arrive. This call also provides the client's address.sendto(): Send a reply back to the client's address.
socket(): Create a UDP socket.sendto(): Create a datagram, attaching the server's IP and port, and send it.recvfrom(): Optionally wait for a reply.
This snippet illustrates the TCP server lifecycle, creating a new socket for each client.
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('', 12000))
server_socket.listen(1)
print("The TCP server is ready to receive")
while True:
connection_socket, addr = server_socket.accept() # New socket created
message = connection_socket.recv(1024).decode()
connection_socket.send(message.upper().encode())
connection_socket.close()These snippets illustrate the connectionless nature of UDP. Note how the server uses a single socket and how addresses are specified in each sendto call.
UDPServer.py
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('', 12000))
print("The UDP server is ready to receive")
while True:
message, client_address = server_socket.recvfrom(2048)
modified_message = message.decode().upper()
server_socket.sendto(modified_message.encode(), client_address)UDPClient.py
import socket
server_name = 'hostname'
server_port = 12000
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
message = input('Input lowercase sentence:')
client_socket.sendto(message.encode(), (server_name, server_port)) # Attach destination addr
modified_message, server_address = client_socket.recvfrom(2048)
print(modified_message.decode())
client_socket.close()- 2-Mark Questions:
- What is the difference between a listening socket and a connection socket on a TCP server?
- Which function call in the TCP socket API triggers the three-way handshake from the client side? (
connect()). - In UDP socket programming, how does the server know where to send a reply? (The client's address and port are part of the datagram received via
recvfrom()).
- 10-Mark Question:
- "Compare the socket programming steps (API calls) required for a TCP application versus a UDP application. Describe the process from both the client and server perspectives."
- Tip: Create two columns, one for TCP and one for UDP. List the sequence of socket calls for the server and client. Highlight the key differences, such as the
listen()/accept()calls in TCP and the lack of aconnect()call in UDP, explaining why these differences exist.
- Tip: Create two columns, one for TCP and one for UDP. List the sequence of socket calls for the server and client. Highlight the key differences, such as the
- "Compare the socket programming steps (API calls) required for a TCP application versus a UDP application. Describe the process from both the client and server perspectives."