SlideShare a Scribd company logo
1 of 33
Download to read offline
Network Programming in C
Networked Systems 3
Laboratory Sessions and Problem Sets
Lab Timetable, Aims, and Objectives
2
Teaching Week Activity
14 Introduction
15 Warm-up exercise
16
Web client
17 Web client
18
Web client
19
Web server
20
Web server
21
Web server
22
Web server
23 -
• Aims and objectives
• To demonstrate how the world-wide
web works, at a protocol level
• To teach concurrent network
programming in C
Relation Between Labs and Lectures
3
18
14 15 16 17 19 20 21 22 23
Week
Layer
1
2
3
4
5
6
7 Lab
Lecture
Network Programming in C:
The Berkeley Sockets API
4
The Berkeley Sockets API
• Widely used low-level C networking API
• First introduced in 4.3BSD Unix
• Now available on most platforms: Linux, MacOS X, Windows, FreeBSD,
Solaris, etc.
• Largely compatible cross-platform
• Recommended reading:
• Stevens, Fenner, and Rudoff, “Unix Network Programming
volume 1: The Sockets Networking API”, 3rd Edition,
Addison-Wesley, 2003.
5
Concepts
Network
Socket
Application • Sockets provide a standard
interface between network
and application
• Two types of socket:
• Stream – provides a virtual circuit service
• Datagram – delivers individual packets
• Independent of network type:
• Commonly used with TCP/IP and UDP/IP,
but not specific to the Internet protocols
• Only discuss TCP/IP sockets today
6
What is a TCP/IP Connection?
• A reliable byte-stream connection between two
computers
• Most commonly used in a client-server fashion:
• The server listens on a well-known port
• The port is a 16-bit number used to distinguish servers
• E.g. web server listens on port 80, email server on port 25
• The client connects to that port
• Once connection is established, either side can write data into the
connection, where it becomes available for the other side to read
• The Sockets API represents the connection using a
file descriptor
7
bind(fd, ..., ...)
Network
Client
int fd = socket(...)
Server
?
listen(fd, ...)
connfd = accept(fd, ...)
read(connfd, buffer, buflen)
write(connfd, data, datalen)
close(connfd)
connect(fd, ..., ...)
write(fd, data, datalen)
read(fd, buffer, buflen)
close(fd)
int fd = socket(...)
Socket
fd
Socket
fd connfd
?
TCP/IP Connection
8
TCP/IP Connection
fd = socket(…);
connect(fd, …);
write(fd, …);
read(fd, …);
close(fd, …);
fd = socket(…);
bind(fd, …);
listen(fd, …);
connfd = accept(fd, …);
read(connfd, …);
write(connfd, …);
read(connfd, …);
close(connfd, …);
TCP/IP connection established
Send request
Wait for response
TCP/IP connection shutdown
EOF read
Block until connection
established
Specify well-known
port
Begin listening
Client
Server
9
Creating a socket
#include <sys/socket.h>
int fd;
...
fd = socket(family, type, protocol);
if (fd == -1) {
// Error: unable to create socket
...
}
...
AF_INET for IPv4
AF_INET6 for IPv6
SOCK_STREAM for TCP
SOCK_DGRAM for UDP
0 (not used for Internet sockets)
Create an unbound socket, not connected to network;
can be used as either a client or a server
#include <sys/types.h>
#include <sys/socket.h>
10
Handling Errors
fd = socket(family, type, protocol);
if (fd == -1) {
switch (errno) {
case EPROTONOSUPPORT :
// Protocol not supported
...
case EACCES:
// Permission denied
...
case ...
default:
// Other error...
...
}
The Unix man pages list
possible errors that can
occur for each function
E.g. do “man 2 socket”
in a terminal, and read the
ERRORS section
11
Socket functions return -1 and set the global variable
errno on failure
Binding a Server Socket
• Bind a socket to a port
on a network interface
• Needed to run servers on a
well-known port - with addr
specified as INADDR_ANY
• Not generally used on clients,
since typically don’t care
which port used
#include <sys/types.h>
#include <sys/socket.h>
...
if (bind(fd, addr, addrlen) == -1) {
// Error: unable to bind
...
}
...
12
Listening for Connections
#include <sys/types.h>
#include <sys/socket.h>
if (listen(fd, backlog) == -1) {
// Error
...
}
...
Tell the socket to listen for new connections
The backlog is the maximum number of connections the
socket will queue up, each waiting to be accept()’ed
13
Connecting to a Server
#include <sys/types.h>
#include <sys/socket.h>
if (connect(fd, addr, addrlen) == -1) {
// Error: unable to open connection
...
}
...
Tries to open a connection to the server
Times out after 75 seconds if no response
Pointer to a struct sockaddr
Size of the struct in bytes
14
Specifying Addresses & Ports
• Must specify the address and port when calling
bind() or connect()
• The address can be either IPv4 or IPv6
• Could be modelled in C as a union, but the designers of the sockets API
chose to use a number of structs, and abuse casting instead
15
struct sockaddr
• Addresses specified via
struct sockaddr
• Has a data field big enough to
hold the largest address of any
family
• Plus sa_len and sa_family
to specify the length and type
of the address
• Treats the address as an opaque
binary string
16
struct sockaddr {
uint8_t sa_len;
sa_family_t sa_family;
char sa_data[22];
};
struct sockaddr_in
17
• Two variations exist for
IPv4 and IPv6
addresses
• Use struct sockaddr_in to
hold an IPv4 address
• Has the same size and memory
layout as struct sockaddr,
but interprets the bits differently
to give structure to the address
struct in_addr {
in_addr_t s_addr;
};
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
char sin_pad[16];
};
struct sockaddr_in6
18
• Two variations exist for
IPv4 and IPv6
addresses
• Use struct sockaddr_in6
to hold an IPv6 address
• Has the same size and memory
layout as struct sockaddr,
but interprets the bits differently
to give structure to the address
struct in6_addr {
uint8_t s6_addr[16];
};
struct sockaddr_in6 {
uint8_t sin6_len;
sa_family_t sin6_family;
in_port_t sin6_port;
uint32_t sin6_flowinfo;
struct in6_addr sin6_addr;
};
Working with Addresses
• Work with either struct sockaddr_in or
struct sockaddr_in6
• Cast it to a struct sockaddr before calling
the socket routines
struct sockaddr_in addr;
...
// Fill in addr here
...
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
...
19
Creating an Address: Manually (Client)
inet_pton() to convert address
htons() to convert port
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
struct sockaddr_in addr;
...
inet_pton(AF_INET, “130.209.240.1”, &addr.sin_addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
...
20
Creating an Address: Manually (Server)
Usually specify INADDR_ANY
htons() to convert port
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
struct sockaddr_in addr;
...
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
...
21
Creating an Address: DNS
• Prefer using DNS names to raw IP addresses
• Use getaddrinfo() to look-up name in DNS
• Returns a linked list of struct addrinfo values, representing
addresses of the host
struct addrinfo {
int ai_flags; // input flags
int ai_family; // AF_INET, AF_INET6, ...
int ai_socktype; // IPPROTO_TCP, IPPROTO_UDP
int ai_protocol; // SOCK_STREAM, SOCK_DRAM, ...
socklen_t ai_addrlen; // length of socket-address
struct sockaddr *ai_addr; // socket-address for socket
char *ai_canonname; // canonical name of host
struct addrinfo *ai_next; // pointer to next in list
};
22
Connecting via a DNS Query
struct addrinfo hints, *ai, *ai0;
int i;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((i = getaddrinfo(“www.google.com”, "80", &hints, &ai0)) != 0) {
printf("Unable to look up IP address: %s", gai_strerror(i));
...
}
for (ai = ai0; ai != NULL; ai = ai->ai_next) {
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd == -1) {
perror("Unable to create socket");
continue;
}
if (connect(fd, ai->ai_addr, ai->ai_addrlen) == -1) {
perror("Unable to connect");
close(fd);
continue;
}
...
}
23
Accepting Connections
#include <sys/types.h>
#include <sys/socket.h>
int connfd;
struct sockaddr_in cliaddr;
socklen_t cliaddrlen = sizeof(cliaddr);
...
connfd = accept(fd, (struct sockaddr *) &cliaddr, &cliaddrlen);
if (connfd == -1) {
// Error
...
}
...
Accepts a connection, returns new file descriptor for the
connection (connfd) and client address (cliaddr)
24
Accepting Connections
• A TCP/IP server may have multiple connections
outstanding
• Can accept() connections one at a time, handling each request in
series
• Can accept() connections and start a new thread for each, allowing it to
process several in parallel
• Each call to accept() returns a new file descriptor
25
Reading and Writing Data
#define BUFLEN 1500
...
ssize_t i;
ssize_t rcount;
char buf[BUFLEN];
...
rcount = read(fd, buf, BUFLEN);
if (rcount == -1) {
// Error has occurred
...
}
...
for (i = 0; i < rcount; i++) {
printf(“%c”, buf[i]);
}
Read up to BUFLEN bytes of
data from connection; blocks
until data available
Returns actual number of
bytes read, or -1 on error
Data is not null terminated
26
Reading and Writing Data
char data[] = “Hello, world!”;
int datalen = strlen(data);
...
if (write(fd, data, datalen) == -1) {
// Error has occurred
...
}
...
Send data on a TCP/IP connection; blocks until all data
can be written
Returns actual number of bytes written, or -1 on error
27
Reading and Writing Data
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char x[] = "Hello, world!";
char *y = malloc(14);
sprintf(y, "Hello, world!");
printf("x = %sn", x);
printf("y = %sn", y);
printf("sizeof(x) = %dn", sizeof(x));
printf("sizeof(y) = %dn", sizeof(y));
printf("strlen(x) = %dn", strlen(x));
printf("strlen(y) = %dn", strlen(y));
return 0;
}
What gets printed?
Why?
28
Closing a Socket
#include <unistd.h>
close(fd);
Close and destroy a socket
Close the file descriptor for each connection, then the file
descriptor for the underlying socket
29
Programming Exercises
30
• Laboratory work is assessed, total weighting 20%
• All students are required to attend Wednesday labs
Assessment
31
Exercise Date set Date due* Weighting
Warm-up 13 January
26 January,
12:00pm
4%
Web client 27 January
16 February,
12:00pm
6%
Web server 17 February
12 March,
12:00pm
10%
* Note: these are hard deadlines; late submissions will receive a mark of zero unless
accompanied by a valid special circumstances form.
Warm-up Exercise
32
• Write two programs in C: hello_client and
hello_server
• The server listens for, and accepts, a single TCP connection; it reads all
the data it can from that connection, and prints it to the screen; then it
closes the connection
• The client connects to the server, sends the string “Hello, world!”, then
closes the connection
• Details on the handout…
Questions?
33

More Related Content

Similar to lab04.pdf

Similar to lab04.pdf (20)

Sockets
Sockets Sockets
Sockets
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
 
sockets_intro.ppt
sockets_intro.pptsockets_intro.ppt
sockets_intro.ppt
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Sockets intro
Sockets introSockets intro
Sockets intro
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
 
Networking chapter III
Networking chapter IIINetworking chapter III
Networking chapter III
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
L5-Sockets.pptx
L5-Sockets.pptxL5-Sockets.pptx
L5-Sockets.pptx
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
 
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYAPYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
 
Basics of sockets
Basics of socketsBasics of sockets
Basics of sockets
 
sockets
socketssockets
sockets
 
Sockets in unix
Sockets in unixSockets in unix
Sockets in unix
 
Network Sockets
Network SocketsNetwork Sockets
Network Sockets
 
Sockets
Sockets Sockets
Sockets
 
Npc08
Npc08Npc08
Npc08
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 

Recently uploaded

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Recently uploaded (20)

★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 

lab04.pdf

  • 1. Network Programming in C Networked Systems 3 Laboratory Sessions and Problem Sets
  • 2. Lab Timetable, Aims, and Objectives 2 Teaching Week Activity 14 Introduction 15 Warm-up exercise 16 Web client 17 Web client 18 Web client 19 Web server 20 Web server 21 Web server 22 Web server 23 - • Aims and objectives • To demonstrate how the world-wide web works, at a protocol level • To teach concurrent network programming in C
  • 3. Relation Between Labs and Lectures 3 18 14 15 16 17 19 20 21 22 23 Week Layer 1 2 3 4 5 6 7 Lab Lecture
  • 4. Network Programming in C: The Berkeley Sockets API 4
  • 5. The Berkeley Sockets API • Widely used low-level C networking API • First introduced in 4.3BSD Unix • Now available on most platforms: Linux, MacOS X, Windows, FreeBSD, Solaris, etc. • Largely compatible cross-platform • Recommended reading: • Stevens, Fenner, and Rudoff, “Unix Network Programming volume 1: The Sockets Networking API”, 3rd Edition, Addison-Wesley, 2003. 5
  • 6. Concepts Network Socket Application • Sockets provide a standard interface between network and application • Two types of socket: • Stream – provides a virtual circuit service • Datagram – delivers individual packets • Independent of network type: • Commonly used with TCP/IP and UDP/IP, but not specific to the Internet protocols • Only discuss TCP/IP sockets today 6
  • 7. What is a TCP/IP Connection? • A reliable byte-stream connection between two computers • Most commonly used in a client-server fashion: • The server listens on a well-known port • The port is a 16-bit number used to distinguish servers • E.g. web server listens on port 80, email server on port 25 • The client connects to that port • Once connection is established, either side can write data into the connection, where it becomes available for the other side to read • The Sockets API represents the connection using a file descriptor 7
  • 8. bind(fd, ..., ...) Network Client int fd = socket(...) Server ? listen(fd, ...) connfd = accept(fd, ...) read(connfd, buffer, buflen) write(connfd, data, datalen) close(connfd) connect(fd, ..., ...) write(fd, data, datalen) read(fd, buffer, buflen) close(fd) int fd = socket(...) Socket fd Socket fd connfd ? TCP/IP Connection 8
  • 9. TCP/IP Connection fd = socket(…); connect(fd, …); write(fd, …); read(fd, …); close(fd, …); fd = socket(…); bind(fd, …); listen(fd, …); connfd = accept(fd, …); read(connfd, …); write(connfd, …); read(connfd, …); close(connfd, …); TCP/IP connection established Send request Wait for response TCP/IP connection shutdown EOF read Block until connection established Specify well-known port Begin listening Client Server 9
  • 10. Creating a socket #include <sys/socket.h> int fd; ... fd = socket(family, type, protocol); if (fd == -1) { // Error: unable to create socket ... } ... AF_INET for IPv4 AF_INET6 for IPv6 SOCK_STREAM for TCP SOCK_DGRAM for UDP 0 (not used for Internet sockets) Create an unbound socket, not connected to network; can be used as either a client or a server #include <sys/types.h> #include <sys/socket.h> 10
  • 11. Handling Errors fd = socket(family, type, protocol); if (fd == -1) { switch (errno) { case EPROTONOSUPPORT : // Protocol not supported ... case EACCES: // Permission denied ... case ... default: // Other error... ... } The Unix man pages list possible errors that can occur for each function E.g. do “man 2 socket” in a terminal, and read the ERRORS section 11 Socket functions return -1 and set the global variable errno on failure
  • 12. Binding a Server Socket • Bind a socket to a port on a network interface • Needed to run servers on a well-known port - with addr specified as INADDR_ANY • Not generally used on clients, since typically don’t care which port used #include <sys/types.h> #include <sys/socket.h> ... if (bind(fd, addr, addrlen) == -1) { // Error: unable to bind ... } ... 12
  • 13. Listening for Connections #include <sys/types.h> #include <sys/socket.h> if (listen(fd, backlog) == -1) { // Error ... } ... Tell the socket to listen for new connections The backlog is the maximum number of connections the socket will queue up, each waiting to be accept()’ed 13
  • 14. Connecting to a Server #include <sys/types.h> #include <sys/socket.h> if (connect(fd, addr, addrlen) == -1) { // Error: unable to open connection ... } ... Tries to open a connection to the server Times out after 75 seconds if no response Pointer to a struct sockaddr Size of the struct in bytes 14
  • 15. Specifying Addresses & Ports • Must specify the address and port when calling bind() or connect() • The address can be either IPv4 or IPv6 • Could be modelled in C as a union, but the designers of the sockets API chose to use a number of structs, and abuse casting instead 15
  • 16. struct sockaddr • Addresses specified via struct sockaddr • Has a data field big enough to hold the largest address of any family • Plus sa_len and sa_family to specify the length and type of the address • Treats the address as an opaque binary string 16 struct sockaddr { uint8_t sa_len; sa_family_t sa_family; char sa_data[22]; };
  • 17. struct sockaddr_in 17 • Two variations exist for IPv4 and IPv6 addresses • Use struct sockaddr_in to hold an IPv4 address • Has the same size and memory layout as struct sockaddr, but interprets the bits differently to give structure to the address struct in_addr { in_addr_t s_addr; }; struct sockaddr_in { uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_pad[16]; };
  • 18. struct sockaddr_in6 18 • Two variations exist for IPv4 and IPv6 addresses • Use struct sockaddr_in6 to hold an IPv6 address • Has the same size and memory layout as struct sockaddr, but interprets the bits differently to give structure to the address struct in6_addr { uint8_t s6_addr[16]; }; struct sockaddr_in6 { uint8_t sin6_len; sa_family_t sin6_family; in_port_t sin6_port; uint32_t sin6_flowinfo; struct in6_addr sin6_addr; };
  • 19. Working with Addresses • Work with either struct sockaddr_in or struct sockaddr_in6 • Cast it to a struct sockaddr before calling the socket routines struct sockaddr_in addr; ... // Fill in addr here ... if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { ... 19
  • 20. Creating an Address: Manually (Client) inet_pton() to convert address htons() to convert port #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> struct sockaddr_in addr; ... inet_pton(AF_INET, “130.209.240.1”, &addr.sin_addr); addr.sin_family = AF_INET; addr.sin_port = htons(80); if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { ... 20
  • 21. Creating an Address: Manually (Server) Usually specify INADDR_ANY htons() to convert port #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> struct sockaddr_in addr; ... addr.sin_addr.s_addr = INADDR_ANY; addr.sin_family = AF_INET; addr.sin_port = htons(80); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { ... 21
  • 22. Creating an Address: DNS • Prefer using DNS names to raw IP addresses • Use getaddrinfo() to look-up name in DNS • Returns a linked list of struct addrinfo values, representing addresses of the host struct addrinfo { int ai_flags; // input flags int ai_family; // AF_INET, AF_INET6, ... int ai_socktype; // IPPROTO_TCP, IPPROTO_UDP int ai_protocol; // SOCK_STREAM, SOCK_DRAM, ... socklen_t ai_addrlen; // length of socket-address struct sockaddr *ai_addr; // socket-address for socket char *ai_canonname; // canonical name of host struct addrinfo *ai_next; // pointer to next in list }; 22
  • 23. Connecting via a DNS Query struct addrinfo hints, *ai, *ai0; int i; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if ((i = getaddrinfo(“www.google.com”, "80", &hints, &ai0)) != 0) { printf("Unable to look up IP address: %s", gai_strerror(i)); ... } for (ai = ai0; ai != NULL; ai = ai->ai_next) { fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (fd == -1) { perror("Unable to create socket"); continue; } if (connect(fd, ai->ai_addr, ai->ai_addrlen) == -1) { perror("Unable to connect"); close(fd); continue; } ... } 23
  • 24. Accepting Connections #include <sys/types.h> #include <sys/socket.h> int connfd; struct sockaddr_in cliaddr; socklen_t cliaddrlen = sizeof(cliaddr); ... connfd = accept(fd, (struct sockaddr *) &cliaddr, &cliaddrlen); if (connfd == -1) { // Error ... } ... Accepts a connection, returns new file descriptor for the connection (connfd) and client address (cliaddr) 24
  • 25. Accepting Connections • A TCP/IP server may have multiple connections outstanding • Can accept() connections one at a time, handling each request in series • Can accept() connections and start a new thread for each, allowing it to process several in parallel • Each call to accept() returns a new file descriptor 25
  • 26. Reading and Writing Data #define BUFLEN 1500 ... ssize_t i; ssize_t rcount; char buf[BUFLEN]; ... rcount = read(fd, buf, BUFLEN); if (rcount == -1) { // Error has occurred ... } ... for (i = 0; i < rcount; i++) { printf(“%c”, buf[i]); } Read up to BUFLEN bytes of data from connection; blocks until data available Returns actual number of bytes read, or -1 on error Data is not null terminated 26
  • 27. Reading and Writing Data char data[] = “Hello, world!”; int datalen = strlen(data); ... if (write(fd, data, datalen) == -1) { // Error has occurred ... } ... Send data on a TCP/IP connection; blocks until all data can be written Returns actual number of bytes written, or -1 on error 27
  • 28. Reading and Writing Data #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char x[] = "Hello, world!"; char *y = malloc(14); sprintf(y, "Hello, world!"); printf("x = %sn", x); printf("y = %sn", y); printf("sizeof(x) = %dn", sizeof(x)); printf("sizeof(y) = %dn", sizeof(y)); printf("strlen(x) = %dn", strlen(x)); printf("strlen(y) = %dn", strlen(y)); return 0; } What gets printed? Why? 28
  • 29. Closing a Socket #include <unistd.h> close(fd); Close and destroy a socket Close the file descriptor for each connection, then the file descriptor for the underlying socket 29
  • 31. • Laboratory work is assessed, total weighting 20% • All students are required to attend Wednesday labs Assessment 31 Exercise Date set Date due* Weighting Warm-up 13 January 26 January, 12:00pm 4% Web client 27 January 16 February, 12:00pm 6% Web server 17 February 12 March, 12:00pm 10% * Note: these are hard deadlines; late submissions will receive a mark of zero unless accompanied by a valid special circumstances form.
  • 32. Warm-up Exercise 32 • Write two programs in C: hello_client and hello_server • The server listens for, and accepts, a single TCP connection; it reads all the data it can from that connection, and prints it to the screen; then it closes the connection • The client connects to the server, sends the string “Hello, world!”, then closes the connection • Details on the handout…