CSC322-Lecture 2-Sockets and
Network Programming
Prepared By :
Dr. Peter Maina Mwangi
SOCKETS
Topics
• Introduction to sockets
• Socket Addresses
• Socket system calls for connection oriented
protocol and connection less protocol
• Example client/server programs
Introduction
• Pipes, FIFOs, UNIX system V, IPC methods (message
queues, shared memory, semaphores) are useful for
process communicating on the same machine, but
they do not support processes running on different
machines to communicate.
• Socket supports IPC with in the same machine and
support processes running on the different machines
to communicate.
• To support IPC over a local area network BSD UNIX 4.2
developed sockets which provide protocol
independent network interface services.
What is socket
• A socket acts as an endpoint in the connection between a
client and a server present in a network.
• Socket acts as an interface between the application and
network.
-The application creates a socket
-The socket type dictates the style of communication
• End point determined by two things:
– Host address: IP address is Network Layer
– Port number: is Transport Layer
• Two end-points determine a connection: socket pair
– ex: 206.62.226.35,p21 + 198.69.10.2,p1500
– ex: 206.62.226.35,p21 + 198.69.10.2,p1499
How Sockets Work
1. What is a Socket?
• A socket is an endpoint for sending or receiving data across a
network.
• It enables two applications (on the same or different devices) to
communicate over the internet or a local network.
– A socket is associated with an IP address and a port number, which
uniquely identifies the connection.
2. Basic Components of a Socket
– IP Address: Identifies the host machine (e.g., 192.168.1.1 or google.com).
– Port Number: A numeric identifier for a specific application running on a
machine (e.g., 80 for HTTP, 443 for HTTPS).
– Protocol: Defines how data is transmitted (TCP for reliable communication,
UDP for fast, connectionless communication).
Connection-Oriented vs.
Connectionless Communication
• In networking, communication between
devices can be classified as connection-
oriented or connectionless, depending on
whether a dedicated connection is established
before data transmission.
Connection-Oriented Communication (TCP)
• Definition: Connection-oriented communication is a
type of communication where a dedicated connection
is established between sender and receiver before
data transfer begins.
• It ensures reliable and ordered delivery of data
packets.
Key Features
✓ Reliable transmission – Ensures data is received correctly.
✓ Order is maintained – Packets arrive in sequence.
✓ Error checking – Detects and retransmits lost packets.
✓ Connection establishment – Uses a three-way handshake
before communication starts.
✓ More overhead – Due to connection setup and
acknowledgments.
Example Protocol: TCP (Transmission
Control Protocol)
TCP is the most widely used connection-
oriented protocol, used in applications like:
• Web Browsing (HTTP/HTTPS)
• Email (SMTP, IMAP, POP3)
• File Transfers (FTP, SFTP)
• Secure Communication (SSH, TLS/SSL)
How TCP Works (Three-Way Handshake)
Before data transmission, TCP establishes a connection using a
three-step process:
Step 1: SYN (Synchronization Request)
• The client sends a SYN packet to request a connection.
Step 2: SYN-ACK (Synchronization Acknowledgment)
• The server responds with a SYN-ACK packet to acknowledge the
request.
Step 3: ACK (Acknowledgment)
• The client replies with an ACK, and the connection is established.
Now data transfer can begin!
TCP Connection Diagram
TCP Connection-Oriented Communication (Python Example)
• TCP Server (Python) • TCP Client (Python)
TCP Connection Termination (Four-Way Handshake)
When communication is complete, TCP terminates the connection
gracefully:
Step 1: FIN
– Client sends a FIN request to close the connection.
Step 2: ACK
– Server acknowledges the FIN.
Step 3: FIN
– Server sends its own FIN to indicate it is also closing.
Step 4: ACK
– Client acknowledges the FIN, and the connection closes.
TCP Disconnection Diagram
Connectionless-Oriented Communication (UDP)
What is Connectionless Communication?
• Connectionless communication is a method of data transmission
where no dedicated connection is established between the sender
and the receiver before communication.
• Each packet (datagram) is sent independently, without
guaranteeing order or delivery.
Key Features of Connectionless Communication
• No connection setup – Data is sent without prior handshake.
Faster than TCP – Lower latency, minimal overhead.
Unreliable delivery – No guarantee that data will reach the
destination.
Out-of-order packets – Data may arrive out of sequence.
No error correction – Lost packets are not retransmitted.
Protocol Example: UDP (User Datagram
Protocol)
• UDP is a commonly used connectionless
protocol, ideal for applications requiring low
latency and fast data transmission.
• Streaming Video (YouTube, Netflix, Twitch)
• Online Gaming (Fortnite, PUBG, Call of Duty)
• VoIP Calls (Skype, Zoom, WhatsApp Calls)
• DNS Lookups (Domain Name System queries)
How UDP Works (No Handshake
Required)
• Unlike TCP, UDP does not establish a connection before sending
data.
UDP Communication Flow
– The client sends a UDP packet to the server.
– The server receives the packet but does not acknowledge
receipt.
– If a packet is lost, no retransmission occurs.
• UDP Communication Diagram
• This makes UDP much faster but less reliable than TCP!
UDP Connectionless Communication (Python Example)
• UDP Server (Python) • UDP Client (Python)
Why Do We Need Sockets?
• Sockets provide a standardized way to enable
communication between devices, whether they
are on the same machine or across the internet.
Importance of Sockets in Network Communication
1. Enables Network Communication Between
Applications
– Sockets allow two or more applications (on the same
device or different devices) to exchange data over a
network.
– Example: Web browsers communicate with web
servers using sockets.
Cont.
2. Supports Internet-Based Communication
• Sockets are the foundation of internet applications such as:
Web Browsing (HTTP/HTTPS)
Emails (SMTP, IMAP, POP3)
File Transfers (FTP, SFTP)
Messaging & Chat Applications (WhatsApp, Telegram)
3. Allows Communication Between Different Systems
(Interprocess Communication - IPC)
• Sockets enable different operating systems and programming
languages to communicate.
• Example: A Python client can communicate with a Java
server using sockets.
Cont.
4. Supports Different Communication Protocols (TCP & UDP)
– TCP Sockets – Ensure reliable, ordered data transmission.
– UDP Sockets – Provide fast, connectionless communication for real-time
applications.
5. Used in Client-Server Models
– Sockets help in building client-server applications, where a server listens for
requests from multiple clients.
Example:
• A web server (Google, Facebook) listens for connections from web browsers
(Chrome, Firefox).
• A game server hosts multiple players in an online multiplayer game.
6. Allows Distributed Systems & IoT Communication
– Sockets enable communication in distributed systems like:
Cloud computing (AWS, Google Cloud)
IoT (Internet of Things) (Smart homes, industrial automation)
Socket and Process Communication
application layer application layer
User Process Internet User Process
Socket Socket
transport
OS layer (TCP/UDP)
network transport layer (TCP/UDP)
OS network
network layer (IP)
Internet network layer (IP)
stack stack
link layer (e.g. ethernet) Internet link layer (e.g. ethernet)
The interface that the OS provides to its networking subsystem
19
How Do Sockets Work?
• Step 1: Creating a Socket
– A socket is created using a programming language like Python,
Java, or C.
• Step 2: Binding to an IP Address & Port
– Each socket is assigned an IP address and a port number to
identify communication endpoints.
• Step 3: Connecting & Sending Data
• Client connects to a server using the socket.
• Data is exchanged using TCP (connection-oriented) or UDP
(connectionless) communication.
• Step 4: Closing the Socket
– Once communication is done, the socket is closed to free up
resources.
Types Sockets
1. Stream Sockets (SOCK_STREAM)
• Function:
– Provides a reliable, connection-oriented communication
channel.
– Uses byte streams to transmit data in order.
– Ensures error detection and data retransmission in case
of loss.
• Protocol Used:
– TCP (Transmission Control Protocol)
• Use Cases:
– Web browsing (HTTP, HTTPS)
– File transfers (FTP, SFTP)
– Emails (SMTP, IMAP, POP3)
Transmission Control Protocol (TCP):
Stream Socket
TCP Telephone
Postal Mail
Call
• Reliable – guarantee delivery •• Guaranteed delivery
Single mailbox to receive
messages
• Byte stream – in-order delivery •• In-order delivery
Unreliable ☺
• Not necessarily in-order
• Connection-oriented – single • Connection-oriented
socket per connection delivery
• Each letter is independent
• Setup connection followed by •• Must connection
Setup address each reply
followed by
data transfer conversation
Example TCP applications
Web, Email, Telnet
22
2. Datagram Sockets (SOCK_DGRAM)
• Function:
– Provides a fast, connectionless communication channel.
– No guarantee of delivery or order of packets (unreliable).
– Suitable for real-time applications where speed is more
important than reliability.
• Protocol Used:
– UDP (User Datagram Protocol)
• Use Cases:
– VoIP calls (Skype, WhatsApp, Zoom)
– Online gaming (Fortnite, PUBG, Call of Duty)
– Video streaming (YouTube, Netflix, Twitch)
– DNS lookups (Domain Name System requests)
User Datagram Protocol (UDP):
Datagram Socket
UDP Postal Mail
• Single socket to receive messages •• Single
Singlemailbox
mailboxto receive letters
to receive
messages
• No guarantee of delivery •• Unreliable
Unreliable ☺
• Not necessarily in-order
• Not necessarily in-order delivery • Not necessarily in-order delivery
delivery
• Datagram – independent packets •• Each
Lettersletter is independent
sent independently
• Must address each reply
• Must address each packet • Must address each mail
Example UDP applications
Multimedia, voice over IP (Skype)
24
3. Raw Sockets (SOCK_RAW)
• Function:
– Allows direct access to lower-level network protocols.
– Used for network monitoring, security testing, and packet
analysis.
– Requires administrative privileges to use.
• Protocols Used:
– ICMP (Internet Control Message Protocol) – Ping requests
– IP (Internet Protocol) – Custom packet handling
– ARP (Address Resolution Protocol) – MAC address resolution
• Use Cases:
– Network scanning and security testing (e.g., Nmap, Wireshark)
– Packet sniffing (e.g., capturing all network traffic)
– Custom packet crafting (e.g., sending fake network packets)
4. Sequenced Packet Sockets
(SOCK_SEQPACKET)
• Function:
– Provides connection-oriented, reliable packet delivery.
– Similar to TCP but sends fixed-size packets instead of a
continuous stream.
– Used in specialized real-time communication where
structured data transmission is required.
• Protocols Used:
– SCTP (Stream Control Transmission Protocol)
• Use Cases:
– Telecommunication systems (VoLTE, 5G networks)
– Messaging services that require message boundaries
– Financial transactions and database replication
5. Unix Domain Sockets (SOCK_STREAM or
SOCK_DGRAM)
• Function:
– Used for inter-process communication (IPC) within the same
machine.
– Faster than TCP/IP sockets because it does not use the network
stack.
• Protocols Used:
– No specific protocol (used for local communication, not
internet-based).
• Use Cases:
– Database communication (MySQL, PostgreSQL use Unix
sockets for efficiency)
– Docker container communication
– Inter-process communication on Unix/Linux systems
6. Bluetooth Sockets (SOCK_STREAM or
SOCK_DGRAM)
• Function:
– Used for wireless communication between Bluetooth-enabled
devices.
– Can be connection-oriented (reliable) or connectionless (fast
but unreliable).
• Protocols Used:
– RFCOMM (for serial data transfer, similar to TCP)
– L2CAP (for high-speed data transfer, similar to UDP)
• Use Cases:
• Bluetooth file sharing
• Wireless headphones, speakers, and IoT devices
• Communication between smartphones and embedded
devices
Clients and Servers
• Client program • Server program
– Running on end host – Running on end host
– Requests service – Provides service
– E.g., Web browser – E.g., Web server
GET /index.html
“Site under construction” 29
Client-Server Communication
• Client “sometimes on” • Server is “always on”
– Initiates a request to the – Handles services requests
server when interested from many client hosts
– E.g., Web browser on your – E.g., Web server for the
laptop or cell phone www.cnn.com Web site
– Doesn’t communicate – Doesn’t initiate contact with
directly with other clients the clients
– Needs to know server’s – Needs fixed, known address
address
30
Client and Server Processes
• Client process
– process that initiates communication
– Initiates a request for a service.
• Server Process
– process that waits to be contacted
– Listens for requests and provides the requested service.
31
Client-Server Model
• The client-server model is the foundation of
network communication, where a server
provides services, and multiple clients
request services.
Real-Life Example of Client-Server
Communication
• Client: A web browser (Google Chrome,
Firefox) requesting a webpage.
• Server: A web server (Google, Facebook,
Amazon) responding with the webpage data.
How Client and Server Processes Work
Communication Flow
• 1️⃣ Server starts and listens for client connections.
• 2️⃣ Client initiates a connection request.
• 3️⃣ Server accepts the connection.
• 4️⃣ Client sends a request (e.g., webpage request, file
request).
• 5️⃣ Server processes the request and sends a
response.
• 6️⃣ Client receives the response and processes it.
• 7️⃣ Connection is closed after the communication is
complete.
Client-Server Communication
Stream Sockets (TCP): Connection-oriented
Server
Create a socket
Bind the socket
(what port am I on?)
Client
Listen for client
(Wait for incoming connections) Create a socket
Connect to server
Accept connection
Send the request
Receive Request
Send response
Receive response 34
Connection-oriented Example
(Stream Sockets -TCP)
Server
socket()
bind()
Client
listen() socket()
connect()
accept()
send()
recv()
send()
recv()
35
Client-Server Communication
Datagram Sockets (UDP): Connectionless
Server
Client
Create a socket
Create a socket
Bind the socket
Bind the socket
Receive Request Send the request
Send response
Receive response
36
Connectionless Example
(Datagram Sockets - UDP)
Server
Client
socket()
socket()
bind()
bind()
sendto()
recvfrom()
sendto()
recvfrom()
37
Socket Addresses in Network
Programming
What is a Socket Address?
• A socket address is a combination of:
– IP Address (Identifies the device on the network)
– Port Number (Identifies the specific service or process on
the device)
• Together, they uniquely identify a communication endpoint in
a network.
Components of a Socket Address
Component Description
Identifies a host on the
IP Address network (e.g.,
192.168.1.10)
Identifies a process or
Port Number service on the host (e.g., 80
for HTTP)
TCP or UDP, defining how
Protocol
communication happens
Types of IP Addresses Used in Sockets
• IPv4 (Internet Protocol version 4)
– Uses 32-bit addresses
– Written in dot-decimal format
– Example: 192.168.1.1, 8.8.8.8 (Google
DNS)
• IPv6 (Internet Protocol version 6)
– Uses 128-bit addresses
– Written in colon-hexadecimal format
– Example: 2001:db8::ff00:42:8329
Types of Socket Addressing
Types of Socket Addressing
1. Internet Socket Addressing (IPv4 & IPv6)
– Uses IP address + Port to identify
endpoints.
– Used in TCP/IP and UDP/IP
communication.
Example of an Internet Socket Address
• IPv4: 192.168.1.1:8080 (IP + Port)
• IPv6: [2001:db8::1]:443
Cont.
Example: Creating an IPv4 Socket in Python
Example: Creating an IPv6 Socket in Python
Well-Known Port Numbers
• Certain ports are reserved for specific services:
Port Number Service Protocol
20, 21 FTP (File Transfer) TCP
22 SSH (Secure Shell) TCP
25 SMTP (Email) TCP
DNS (Domain Name
53 UDP
System)
80 HTTP (Web Browsing) TCP
HTTPS (Secure Web
443 TCP
Browsing)
3306 MySQL Database TCP
Socket Identification in Network Programming
What is Socket Identification?
• A socket is uniquely identified in a network by a
combination of:
– IP Address → Identifies the host (device) in the
network
– Port Number → Identifies a specific service or
application on the host
– Protocol → Defines how communication occurs (TCP
or UDP)
• Together, these three elements create a unique
identifier for each communication endpoint.
Activity
1.Discuss the socket programming process
for both client and server applications.
Provide examples of use cases.
2.CAP theorem and its implications in
distributed systems. Provide examples of
real-world applications.