1 of 26

CS 39006: Computer Networks Laboratory

POSIX Network Programming: UDP Sockets

INDIAN INSTITUTE OF TECHNOLOGY

KHARAGPUR

Sandip Chakraborty

sandipc@cse.iitkgp.ac.in

Department of Computer Science and Engineering

Abhijnan Chakraborty

abhijnan@cse.iitkgp.ac.in

2 of 26

Overview

Why Sockets?

  • Enable process-to-process communication
  • Basis of:
    • Web (HTTP)
    • DNS
    • Streaming
    • Multiplayer games
    • IoT & sensor systems

Today’s Focus

  • POSIX sockets fundamentals
  • UDP (datagram) sockets
  • Writing a UDP client and server in ANSI C

Hardware

OS

APP11

APP12

Hardware

OS

APP11

APP12

Machine 1

Machine 1

Protocol Stack

Protocol Stack

Indian Institute of Technology Kharagpur

3 of 26

What Are POSIX Sockets?

Definition

  • POSIX sockets = standardized API for network communication
  • Same interface across:
    • Linux
    • macOS
    • UNIX-like systems

Indian Institute of Technology Kharagpur

4 of 26

What Are POSIX Sockets?

Definition

  • POSIX sockets = standardized API for network communication
  • Same interface across:
    • Linux
    • macOS
    • UNIX-like systems

Key Idea:

A socket is an endpoint for communication

Indian Institute of Technology Kharagpur

5 of 26

What Are POSIX Sockets?

Definition

  • POSIX sockets = standardized API for network communication
  • Same interface across:
    • Linux
    • macOS
    • UNIX-like systems

Abstraction

  • File-descriptor–like interface
  • Uses standard system calls

Key Idea:

A socket is an endpoint for communication

Indian Institute of Technology Kharagpur

6 of 26

Socket Communication Model

Components

  • IP Address → identifies host
  • Port Number → identifies application
  • Protocol → TCP or UDP

Indian Institute of Technology Kharagpur

7 of 26

Socket Communication Model

Components

  • IP Address → identifies host
  • Port Number → identifies application
  • Protocol → TCP or UDP

Socket Address

(IP address, Port, Protocol)

Indian Institute of Technology Kharagpur

8 of 26

Socket Communication Model

Components

  • IP Address → identifies host
  • Port Number → identifies application
  • Protocol → TCP or UDP

Socket Address

(IP address, Port, Protocol)

Typical Flow

Process → Socket → Network → Socket → Process

Indian Institute of Technology Kharagpur

9 of 26

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

10 of 26

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?

  • Real-time systems
  • DNS
  • Video/Audio streaming
  • Sensor data, telemetry

Indian Institute of Technology Kharagpur

11 of 26

UDP (Datagram) Sockets

Key Characteristics

  • No connection setup
  • Each message is an independent datagram
  • Application handles:
    • Loss
    • Duplication
    • Ordering

Indian Institute of Technology Kharagpur

12 of 26

UDP (Datagram) Sockets

Key Characteristics

  • No connection setup
  • Each message is an independent datagram
  • Application handles:
    • Loss
    • Duplication
    • Ordering

POSIX Socket Type�

SOCK_DGRAM

Indian Institute of Technology Kharagpur

13 of 26

UDP Socket API (High-Level)

Core System Calls

  • socket()
  • bind()
  • sendto()
  • recvfrom()
  • close()

Indian Institute of Technology Kharagpur

14 of 26

UDP Socket API (High-Level)

Core System Calls

  • socket()
  • bind()
  • sendto()
  • recvfrom()
  • close()

Header Files

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>

Indian Institute of Technology Kharagpur

15 of 26

Creating a UDP Socket

int sockfd;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);

Parameters

  • AF_INET → IPv4
  • SOCK_DGRAM → UDP
  • 0 → default protocol (UDP)

Indian Institute of Technology Kharagpur

16 of 26

Creating a UDP Socket

int sockfd;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);

Parameters

  • AF_INET → IPv4
  • SOCK_DGRAM → UDP
  • 0 → default protocol (UDP)

Error Handling

if (sockfd < 0) {

perror("socket failed"); }

Indian Institute of Technology Kharagpur

17 of 26

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

18 of 26

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()?�

  • Converts host byte order → network byte order

Indian Institute of Technology Kharagpur

19 of 26

UDP Server: Binding the Socket

Purpose

  • Associate socket with a port

bind(sockfd,

(struct sockaddr *)&server_addr,

sizeof(server_addr));

  • Error Check

if (bind(...) < 0) {

perror("bind failed");

}

Indian Institute of Technology Kharagpur

20 of 26

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

21 of 26

Sending Data (UDP Server)

sendto(sockfd,

"Hello Client",

12,

0,

(struct sockaddr *)&client_addr,

len);

Note

  • Server replies using client’s address from recvfrom()

Indian Institute of Technology Kharagpur

22 of 26

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

23 of 26

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

24 of 26

UDP Client Basics

Differences from Server

  • No bind() required (OS assigns port)
  • Directly uses sendto()

Indian Institute of Technology Kharagpur

25 of 26

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

26 of 26

Common Pitfalls

  • Forgetting htons() / inet_pton()
  • Assuming delivery is guaranteed
  • Buffer overflows
  • Not checking return values
  • Firewall blocking UDP ports

Indian Institute of Technology Kharagpur