Download Network Programming: Understanding Clients and Servers in Computer Systems - Prof. Mirela and more Study notes Operating Systems in PDF only on Docsity!
CSC 2400: Computer Systems
Network Programming
Lecture Goals
z Programmer’s view of the Internet
z Sockets interface
z Writing clients and servers
End System: Computer on the ‘Net
Internet
Also known as a “host”…
Clients and Servers
Client program
Running on end host
Requests service
E.g., Web browser
Server program
Running on end host
Provides service
E.g., Web server
GET /index.html
“Site under construction”
Delivering the Data: Division of Labor
Network
Deliver data packet to the destination host
Based on the destination IP address
Operating system
Deliver data to the destination socket
Based on the destination port number
Application
Read data from and write data to the socket
Interpret the data (e.g., render a Web page)
A Programmer’s View of the Internet
1. Hosts are mapped to a set of 32-bit IP addresses.
2. The set of IP addresses is mapped to a set of identifiers
called Internet domain names.
128.2.203.179 is mapped to www.cs.cmu.edu
3. Internet sockets are communication endpoints.
4. A process on one Internet host can communicate with a
process on another Internet host over a connection.
1. IP Addresses
32-bit IP addresses are stored in an IP address struct
IP addresses are always stored in memory in network byte order
(big-endian byte order)
True in general for any integer transferred in a packet header
from one machine to another.
z E.g., the port number used to identify an Internet connection.
/* Internet address structure / struct in_addr { unsigned int s_addr; / network byte order (big-endian) */ };
Handy network byte-order conversion functions:
htonl: convert long int from host to network byte order.
htons: convert short int from host to network byte order.
ntohl: convert long int from network to host byte order.
ntohs: convert short int from network to host byte order.
2. Domain Naming System (DNS)
The Internet maintains a mapping between IP addresses and
domain names in a huge worldwide distributed database
called DNS.
Conceptually, programmers can view the DNS database as a
collection of millions of host entry structures :
Functions for retrieving host entries from DNS:
gethostbyname: query key is a DNS domain name.
gethostbyaddr: query key is an IP address.
/* DNS host entry structure */ struct hostent { char h_name; / official domain name of host */ char *h_aliases; / null-terminated array of domain names / int h_addrtype; / host address type (AF_INET) / int h_length; / length of an address, in bytes */ char *h_addr_list; / null-terminated array of in_addr structs */ };
4. Internet Connections
Connection socket pair (128.2.194.242:51213, 208.216.181.15:80)
Server (port 80) Client
Client socket address 128.2.194.242:
Server socket address 208.216.181.15:
Client host address 128.2.194.
Server host address 208.216.181.
Clients and servers communicate by sending streams of
bytes over connections.
Connections are point-to-point, full-duplex (2-way
communication), and reliable.
Note: 51213 is an ephemeral port allocated by the kernel
Note: 80 is a well-known port associated with Web servers
Clients
Examples of client programs
Web browsers, ftp, telnet, ssh
How does a client find the server?
The IP address in the server socket address identifies the host
(more precisely, an adapter on the host)
The (well-known) port in the server socket address identifies the
service, and thus implicitly identifies the server process that
performs that service.
Examples of well know ports
z Port 7: Echo server
z Port 23: Telnet server
z Port 25: Mail server
z Port 80: Web server
Well-Known vs. Ephemeral Ports
Server has a well-known port (e.g., port 80)
Between 0 and 1023
Client picks an unused ephemeral (i.e., temporary) port
Between 1024 and 65535
See http://www.iana.org/assignments/port-numbers
Servers
Servers are long-running processes (daemons).
Created at boot-time (typically) by the init process (process 1)
Run continuously until the machine is turned off.
Each server waits for requests to arrive on a well-known port
associated with a particular service.
Port 7: echo server
Port 23: telnet server
Port 25: mail server
Port 80: HTTP server
A machine that runs a server process is also often referred to
as a “server.”
See /etc/services for a
comprehensive list of the
services available on a
Linux machine.
Sockets Interface
Created in the early 80’s as part of the original Berkeley
distribution of Unix that contained an early version of the
Internet protocols.
Provides a user-level interface to the network.
Underlying basis for all Internet applications.
Based on client/server programming model.
Overview of the Sockets Interface
Client (^) Server
socket socket
bind
listen
accept
read
read
write
close
read
connect
write
close
Connection request
EOF
Await connection request from next client
open_listenfd
open_clientfd
Sockets Interface
socket
First step in making a network connection: create a socket
bind
The server's second step in making a network connection: bind
the socket so it can listen
connect
The client's second step in making a network connection: connect
(call) on the socket
listen
The server's third step in making a network connection: listen for
calls on the socket
accept
The server's fourth step in making a network connection: accept
an incoming connection (call) on the socket
Read and Write
Once the connection is established, both the server and
client can use the read and write system calls to
transmit data back and forth
Typical Server Program
Prepare to communicate
Create a socket
Associate local address and port with the socket
Wait to hear from a client (passive open)
Indicate how many clients-in-waiting to permit
Accept an incoming connection from a client
Exchange data with the client over new socket
Receive data from the socket
Do stuff to handle the request (e.g., get a file)
Send data to the socket
Close the socket
Repeat with the next connection request
Putting It all Together …
Client (^) Server
socket socket
bind
listen
accept
read
read
write
close
read
connect
write
close
Connection request
EOF
Await connection request from next client
open_listenfd
open_clientfd
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.
Internet-specific socket address:
Must cast (sockaddr_in *) to (sockaddr *) for connect,
bind, and accept.
struct sockaddr { unsigned short sa_family; /* protocol family / char sa_data[14]; / address data. */ };
struct sockaddr_in { unsigned short sin_family; /* address family (always AF_INET) / unsigned short sin_port; / port num in network byte order / struct in_addr sin_addr; / IP addr in network byte order / unsigned char sin_zero[8]; / pad to sizeof(struct sockaddr) */ };
Echo Client: Main Routine
#include “nethelp.h"
/* usage: ./echoclient host port */ int main(int argc, char **argv) { int clientfd, port; char *host, buf[MAXLINE]; rio_t rio;
host = argv[1]; port = atoi(argv[2]);
clientfd = open_clientfd(host, port);
while (fgets(buf, MAXLINE, stdin) != NULL) { write(clientfd, buf, strlen(buf)); n = (clientfd, buf, MAXLINE); write(1, buf, n); } close(clientfd); exit(0); }
Must be implemented. Not built-in.
readline
accept() blocks waiting for a connection request.
accept returns a connected descriptor (connfd) with the
same properties as the listening descriptor (listenfd)
Returns when the connection between client and server is
created and ready for I/O transfers.
All I/O with the client will be done via the connected socket.
accept also fills in client’s IP address.
Echo Server: accept
int listenfd; /* listening descriptor / int connfd; / connected descriptor */ struct sockaddr_in clientaddr; int clientlen;
clientlen = sizeof(clientaddr); connfd = accept(listenfd, (SA *)&clientaddr, &clientlen);
Echo Server: accept Illustrated
listenfd(3)
Client
1. Server blocks in accept,
waiting for connection
request on listening
clientfd descriptor^ listenfd.
Server
listenfd(3)
Client
clientfd
Server
2. Client makes connection
request by calling and blocking in
connect.
Connection request
listenfd(3)
Client
clientfd
Server
3. Server returns connfd from
accept. Client returns from
connect. Connection is now
established between clientfd
and connfd.
connfd(4)
Connected vs. Listening Descriptors
Listening descriptor
End point for client connection requests.
Created once and exists for lifetime of the server.
Connected descriptor
End point of the connection between client and server.
A new descriptor is created each time the server accepts a
connection request from a client.
Exists only as long as it takes to service client.
Why the distinction?
Allows for concurrent servers that can communicate over many
client connections simultaneously.
z E.g., Each time we receive a new request, we create a new
thread to handle the request.
Echo Server: echo
void echo(int connfd) { size_t n; char buf[MAXLINE];
while((n = readline(connfd, buf, MAXLINE)) != 0) { printf("server received %d bytes\n", n); write(connfd, buf, n); } }
The server reads and echoes text lines until EOF (end-of-
file) is encountered.
EOF notification caused by client calling close(clientfd).
IMPORTANT: EOF is a condition, not a particular data byte.
Running the Echo Client and Server
bash$ echoserver 5000
In a separate terminal window:
bash$ echoclient tanner 5000 123 123
bash$
Next …
z Creating our own clients and servers
z Complete the lab posted on the class website