09 Systems Programming-Network Programming
09 Systems Programming-Network Programming
Network Programming
A Client-Server Transaction
• Most network application are based on client-server model
• A server process and one or more client processes
• Server manages some resource
• Server provides services to client by managing client resources
• Server activated by request from client
Computer Network
• A network is a hierarchical system of boxes and wires organized by
geographical proximity
• SAN (System Area Network) spans cluster or machine room Switched
Ethernet, Quadrics QSW, …
• LAN (Local Area Network) spans a building or campus Ethernet is most
prominent example
• WAN (Wide Area Network) spans country or world Typically high-‐speed
point-‐to-‐point phone lines
• An internetwork (internet) is an interconnected set of networks The
Global IP Internet (uppercase “I”) is the most famous example of an
internet (lowercase “i”)
• Let’s see how an internet is built from the ground up
Lowest Level : Ethernet Segment
• Ethernet segment consists of a collection of hosts connected by wires
(twisted pairs) to a hub
• Spans room or floor in a building
• Operation
• Each Ethernet adapter has a unique 48-‐bit address (MAC address)
• E.g., 00:16:ea:e3:54:e6
• Hosts send bits to any other host in chunks called frames
• Hub slavishly copies each bit from each port to every other port
• Every host sees every bit
• Note: Hubs are on their way out. Bridges (switches, routers) became cheap enough to
replace them
Next Level : Bridged Ethernet Segment
• Spans building or campus
• Bridges cleverly learn which hosts are reachable from which ports and
then selectively copies frames from port to port
• The main distinction between regular file I/O and socket I/O is how the
application “opens” the socket descriptors
Socket Address Structures
• Generic socket address:
• For address arguments to connect, bind, and accept
• Necessary only because C did not have generic (void *) pointers when the
sockets interface was designed
• For casting convenience, we adopt the Stevens convention:
struct sockaddr {
uint16_t sa_family; /* Protocol family */
char sa_data[14]; /* Address data. */
};
Socket Address Structures
• Internet-specific socket address IPv4: Must cast (struct Internet-specific socket address IPv6
sockaddr_in *) to (struct sockaddr *) for functions that
take socket address arguments. struct in6_addr {
• Each addrinfo struct returned by getaddrinfo contains arguments that can be passed directly to socket function.
• Also points to a socket address struct that can be passed directly to connect and bind functions.
Host and Service Conversion: getnameinfo()
int getnameinfo( • getnameinfo displays a socket
const struct sockaddr *sa, socklen_t salen, /* address to the corresponding
In: socket addr */
host (name or IP) and service
char *host, size_t hostlen, /* Out: host */
(service or port).
char *serv, size_t servlen, /* Out: service */
• Replaces obsolete gethostbyaddr
int flags); /* optional flags */ and getservbyport funcs.
flags = NI_NUMERICHOST | NI_NUMERICSERV; • Reentrant and protocol
/* Display address string instead of domain
name and port number instead of service
independent.
name */
getaddrinfo() example
NetworkProgramming\hostinfo.c If we disable line
$ ./hostinfo.out www.daiict.ac.in #define IPv4 1, it will provide IPv4 as well as IPv6 addresses
220.226.182.128 → Returned IPv4 IP address $ ./hostinfo.out www.google.com
$ ./hostinfo.out localhost 172.217.163.100
127.0.0.1 2404:6800:4007:811::2004
$ ./hostinfo.out www.twitter.com
104.244.42.65 If we enable line
104.244.42.1 #define IPv4 1, it will provide IPv4 addresses only
$ ./hostinfo.out www.google.com $ ./hostinfo.out www.google.com
172.217.163.100 172.217.163.100
2404:6800:4007:809::2004
$ ./hostinfo.out www.facebook.com
157.240.13.35 → Returned IPv4 IP address
2a03:2880:f10c:83:face:b00c:0:25de → Returned IPv6 IP
address
Client Server Communication
using Socket Interface
Host and Service Conversion: getaddrinfo()
• Clients: Using server IP/hostname
and service/port calls getaddrinfo.
It walks through the returned list of
server socket addresses, trying
each socket address in turn, until
the calls to socket and connect
succeed.
• Server: Using service/port calls
getaddrinfo. It walks through the
returned list of socket addresses
(possible to have different IP) until
calls to socket and bind succeed.
Socket Interface
AF_PACKET Low level packet interface SOCK_RDM Provides a reliable datagram layer that
does not guarantee ordering
SOCK_PACKET Obsolete and should not be used in new
programs
Socket Interface: bind()
Socket Interface: bind()
• A server uses bind to ask the kernel to associate the server’s socket
address with a socket descriptor:
int bind(int listenfd, struct sockaddr *srv_addr, socklen_t
srv_addrlen);
• The process can read bytes that arrive on the connection whose
endpoint is srv_addr by reading from descriptor listenfd.
• Similarly, writes to listenfd are transferred along connection whose
endpoint is srv_addr.
• Best practice is to use getaddrinfo to supply the arguments srv_addr
and addrlen
struct sockaddr: Casted Address for any of the
following types
Name Purpose sockaddr variants
• Usage:
• $ telnet <host> <portnumber>
• Creates a connection with a server running on <host> and listening on port
<portnumber>
Echo Server Problem
• Echo Server is able to handle only 1 client connection at a time
because the main thread goes in loop unless client end the
connection
• How to fix it?
Solution of Echo Server Problem
while (1) {
printf("Waiting for a new Client to connect\n");
clientlen = sizeof(struct sockaddr_storage); /* Important! */
connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clientlen);
if (fork() == 0) /* child will handle a new client everytime server accepts a connection from client */
{
getnameinfo((struct sockaddr *) &clientaddr, clientlen, client_hostname, MAXLINE, client_port, MAXLINE, 0);
printf("Connected to (%s, %s)\n", client_hostname, client_port);
printf("Start Communication with Client\n");
echo(connfd);
printf("End Communication with Client\n");
close(connfd);
}
}
Solution of Echo Server Problem
• Would it be better to use multiple threads or multiple processes to
handle clients?
• What are the challenges with each scheme?