CS 39006: Computer Networks Laboratory
POSIX Network Programming: UDP Sockets
INDIAN INSTITUTE OF TECHNOLOGY
KHARAGPUR
Department of Computer Science and Engineering
Overview
Why Sockets?
Today’s Focus
Hardware
OS
APP11
APP12
Hardware
OS
APP11
APP12
Machine 1
Machine 1
Protocol Stack
Protocol Stack
Indian Institute of Technology Kharagpur
What Are POSIX Sockets?
Definition
Indian Institute of Technology Kharagpur
What Are POSIX Sockets?
Definition
Key Idea:
A socket is an endpoint for communication
Indian Institute of Technology Kharagpur
What Are POSIX Sockets?
Definition
Abstraction
Key Idea:
A socket is an endpoint for communication
Indian Institute of Technology Kharagpur
Socket Communication Model
Components
Indian Institute of Technology Kharagpur
Socket Communication Model
Components
Socket Address
(IP address, Port, Protocol)
Indian Institute of Technology Kharagpur
Socket Communication Model
Components
Socket Address
(IP address, Port, Protocol)
Typical Flow
Process → Socket → Network → Socket → Process
Indian Institute of Technology Kharagpur
TCP vs UDP
Feature | TCP | UDP |
Connection | Connection-oriented | Connectionless |
Reliability | Guaranteed | Best-effort |
Ordering | Yes | No |
Overhead | High | Low |
Speed | Slower | Faster |
Indian Institute of Technology Kharagpur
TCP vs UDP
Feature | TCP | UDP |
Connection | Connection-oriented | Connectionless |
Reliability | Guaranteed | Best-effort |
Ordering | Yes | No |
Overhead | High | Low |
Speed | Slower | Faster |
When to Use UDP?
Indian Institute of Technology Kharagpur
UDP (Datagram) Sockets
Key Characteristics
Indian Institute of Technology Kharagpur
UDP (Datagram) Sockets
Key Characteristics
POSIX Socket Type�
SOCK_DGRAM
Indian Institute of Technology Kharagpur
UDP Socket API (High-Level)
Core System Calls
Indian Institute of Technology Kharagpur
UDP Socket API (High-Level)
Core System Calls
Header Files
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
Indian Institute of Technology Kharagpur
Creating a UDP Socket
int sockfd;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
Parameters
Indian Institute of Technology Kharagpur
Creating a UDP Socket
int sockfd;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
Parameters
Error Handling
if (sockfd < 0) {
perror("socket failed"); }
Indian Institute of Technology Kharagpur
Socket Address Structure
struct sockaddr_in server_addr;
Fields:
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = INADDR_ANY;
Indian Institute of Technology Kharagpur
Socket Address Structure
struct sockaddr_in server_addr;
Fields:
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = INADDR_ANY;
Why htons()?�
Indian Institute of Technology Kharagpur
UDP Server: Binding the Socket
Purpose
bind(sockfd,
(struct sockaddr *)&server_addr,
sizeof(server_addr));
if (bind(...) < 0) {
perror("bind failed");
}
Indian Institute of Technology Kharagpur
Receiving Data (UDP Server)
char buffer[1024];
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
int n = recvfrom(sockfd,
buffer,
sizeof(buffer),
0,
(struct sockaddr *)&client_addr,
&len);
Indian Institute of Technology Kharagpur
Sending Data (UDP Server)
sendto(sockfd,
"Hello Client",
12,
0,
(struct sockaddr *)&client_addr,
len);
Note
Indian Institute of Technology Kharagpur
Minimal UDP Server (ANSI C)
int sockfd;
char buffer[1024];
struct sockaddr_in server_addr, client_addr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = INADDR_ANY;
Indian Institute of Technology Kharagpur
Minimal UDP Server (ANSI C)
bind(sockfd,
(struct sockaddr *)&server_addr,
sizeof(server_addr));
socklen_t len = sizeof(client_addr);
recvfrom(sockfd, buffer, sizeof(buffer), 0,
(struct sockaddr *)&client_addr, &len);
sendto(sockfd, "ACK", 3, 0,
(struct sockaddr *)&client_addr, len);
close(sockfd);
Indian Institute of Technology Kharagpur
UDP Client Basics
Differences from Server
Indian Institute of Technology Kharagpur
UDP Client Code
int sockfd;
struct sockaddr_in server_addr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
sendto(sockfd, "Hello Server", 12, 0, (struct sockaddr *)&server_addr,
sizeof(server_addr));
close(sockfd);
Indian Institute of Technology Kharagpur
Common Pitfalls
Indian Institute of Technology Kharagpur