Lab 04
Lab 04
Networked Systems 3
Laboratory Sessions and Problem Sets
Lab Timetable, Aims, and Objectives
Teaching Week
14
Activity
Introduction
• Aims and objectives
15 Warm-up exercise
• To demonstrate how the world-wide
web works, at a protocol level
16 • To teach concurrent network
17 Web client programming in C
18
19
20
Web server
21
22
23 -
2
Relation Between Labs and Lectures
Layer
7 Lab
6
Lecture
5
4
3
2
1
Week
14 15 16 17 18 19 20 21 22 23
3
Network Programming in C:
The Berkeley Sockets API
4
The Berkeley Sockets API
• Recommended reading:
• Stevens, Fenner, and Rudoff, “Unix Network Programming
volume 1: The Sockets Networking API”, 3rd Edition,
Addison-Wesley, 2003.
5
Concepts
6
What is a TCP/IP Connection?
7
TCP/IP Connection
Client Server
fd
fd connfd
Network ?
?
Socket Socket
close(fd) 8 close(connfd)
TCP/IP Connection
Server fd = socket(…);
close(connfd, …);
9
Creating a socket
#include <sys/types.h>
<sys/socket.h>
#include <sys/socket.h> AF_INET for IPv4
AF_INET6 for IPv6
int fd;
...
fd = socket(family, type, protocol); SOCK_STREAM for TCP
if (fd == -1) { SOCK_DGRAM for UDP
// Error: unable to create socket
... 0 (not used for Internet sockets)
}
...
10
Handling Errors
12
Listening for Connections
#include <sys/types.h>
#include <sys/socket.h>
13
Connecting to a Server
#include <sys/types.h>
Pointer to a struct sockaddr
#include <sys/socket.h>
Size of the struct in bytes
if (connect(fd, addr, addrlen) == -1) {
// Error: unable to open connection
...
}
...
14
Specifying Addresses & Ports
15
struct sockaddr
16
struct sockaddr_in
struct in_addr {
• Two variations exist for };
in_addr_t s_addr;
17
struct sockaddr_in6
struct in6_addr {
• Two variations exist for };
uint8_t s6_addr[16];
18
Working with Addresses
19
Creating an Address: Manually (Client)
#include <sys/types.h>
#include <sys/socket.h> inet_pton() to convert address
#include
#include
<netinet/in.h>
<arpa/inet.h>
htons() to convert port
20
Creating an Address: Manually (Server)
#include <sys/types.h>
#include <sys/socket.h> Usually specify INADDR_ANY
#include <netinet/in.h> htons() to convert port
#include <arpa/inet.h>
21
Creating an Address: DNS
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));
...
}
#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
...
}
...
25
Reading and Writing Data
26
Reading and Writing Data
#include <stdio.h>
#include <stdlib.h> What gets printed?
#include <string.h>
int main()
{
char x[] = "Hello, world!";
char *y = malloc(14);
return 0;
}
28
Closing a Socket
#include <unistd.h>
close(fd);
Close the file descriptor for each connection, then the file
descriptor for the underlying socket
29
Programming Exercises
30
Assessment
26 January,
Warm-up 13 January 4%
12:00pm
16 February,
Web client 27 January 6%
12:00pm
12 March,
Web server 17 February 10%
12:00pm
* Note: these are hard deadlines; late submissions will receive a mark of zero unless
accompanied by a valid special circumstances form.
31
Warm-up Exercise
32
Questions?
33