C++ Sockets TCP
C++ Sockets TCP
Networked Systems 3
Laboratory Sessions
The Berkeley Sockets API
• Recommended reading:
• Stevens, Fenner, and Rudoff, “Unix Network Programming
volume 1: The Sockets Networking API”, 3rd Edition,
Addison-Wesley, 2003.
2
Concepts
3
What is a TCP/IP Connection?
4
TCP/IP Connection
Client Server
fd
fd connfd
Network ?
?
Socket Socket
close(fd) 5 close(connfd)
TCP/IP Connection
Server fd = socket(…);
close(connfd, …);
6
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)
}
...
7
Handling Errors
8
Binding a Server Socket
9
Listening for Connections
#include <sys/types.h>
#include <sys/socket.h>
10
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
...
}
...
11
Specifying Addresses & Ports
12
struct sockaddr
13
struct sockaddr_in
struct in_addr {
• Two variations exist for
};
in_addr_t s_addr;
14
struct sockaddr_in6
struct in6_addr {
• Two variations exist for
};
uint8_t s6_addr[16];
15
Working with Addresses
16
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
17
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>
18
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
};
19
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));
...
}
20
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
...
}
...
22
Reading and Writing Data
23
Handling Multiple Sockets
#include <sys/select.h> The select() call tells you which of a
...
int fd1, fd2;
group of sockets has data available to read
fd_set rfds;
struct timeval timeout;
...
timeout.tv_sec = 1; // 1 second timeout
timeout.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(fd1, &rfds);
FD_SET(fd2, &rfds);
24
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;
}
26
Closing a Socket
#include <unistd.h>
close(fd);
Close the file descriptor for each connection, then the file
descriptor for the underlying socket
27
Questions?
28