Page 1Network ProgrammingJan 20, 2004Topics Programmer’s view of the Internet Sockets interface Writing clients and servers Concurrency with I/O multiplexing Debugging With GDB Version Control With RCSclass03.ppt15-441(Borrowing heavily from 15-213)215-441, Spring 2004A Client-Server TransactionClientprocessServerprocess1. Client sends request2. Server handlesrequest3. Server sends response4. Client handlesresponseResourceEvery network application is based on the client-server model: A server process and one or more client processes Server manages some resource. Server provides service by manipulating resource for clients.Note: clients and servers are processes running on hosts (can be the same or different hosts).Page 2315-441, Spring 2004Network ApplicationsAccess to Network via Program Interface Sockets make network I/O look like files Call system functions to control and communicate Network code handles issues of routing, reliability, ordering, etc.Client ComputerOSNetworkInterfaceClientAppl.SocketOS +NetworkAPIsServer ComputerOSNetworkInterfaceServerAppl.SocketOS +NetworkAPIsInternet415-441, Spring 2004Internet Connections (TCP/IP)Connection socket pair(128.2.194.242:3479, 208.216.181.15:80)Server(port 80)ClientClient socket address128.2.194.242:3479Server socket address208.216.181.15:80Client host address128.2.194.242Server host address208.216.181.15Clients and servers communicate by sending streams of bytes over connections.Connections are point-to-point, full-duplex (2-way communication), and reliable.Note: 3479 is anephemeral port allocatedby the kernel Note: 80 is a well-known portassociated with Web serversPage 3515-441, Spring 2004ClientsExamples of client programs Web browsers, ftp, telnet, sshHow does a client find the server? The IP address in the server socket address identifies the host (more precisely, an adaptor 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 known ports Port 7: Echo server Port 23: Telnet server Port 25: Mail server Port 80: Web server615-441, Spring 2004Using Ports to Identify ServicesWeb server(port 80)Client hostServer host 128.2.194.242Echo server(port 7)Service request for128.2.194.242:80(i.e., the Web server)Web server(port 80)Echo server(port 7)Service request for128.2.194.242:7(i.e., the echo server)KernelKernelClientClientPage 4715-441, Spring 2004ServersServers 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 serverA 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.815-441, Spring 2004Sockets InterfaceCreated 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.Page 5915-441, Spring 2004Client / ServerSessionOverview of the Sockets InterfaceClientServersocket socketbindlistenreadwritereadwriteConnectionrequestreadclosecloseEOFAwait connectionrequest fromnext clientopen_listenfdopen_clientfdacceptconnect1015-441, Spring 2004SocketsWhat is a socket? To the kernel, a socket is an endpoint of communication. To an application, a socket is a file descriptor that lets the application read/write from/to the network. Remember: All Unix I/O devices, including networks, are modeled as files.Clients and servers communicate with each by reading from and writing to socket descriptors.The main distinction between regular file I/O and socket I/O is how the application “opens” the socket descriptors.Page 61115-441, Spring 2004Socket Programming ClichesNetwork Byte Ordering Network is big-endian, host may be big- or little-endian Functions work on 16- and 32-bit values htons() / htonl() : convert host byte order to network byte order ntohs() / ntohl(): convert network byte order to host byte order Use these to convert network addresses, ports, …Structure Casts You will see a lot of ‘structure casts’struct sockaddr_in serveraddr; /* fill in serveraddr with an address */…/* Connect takes (struct sockaddr *) as its second argument */ connect(clientfd, (struct sockaddr *) &serveraddr,sizeof(serveraddr)); …1215-441, Spring 2004Socket Address StructuresGeneric 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) */ };Page 71315-441, Spring 2004Reliable I/O (RIO) SummaryI/O Package Developed by David O’Hallaron http://csapp.cs.cmu.edu/public/code.html (csapp.{h,c}) Allows mix of buffered and unbuffered I/OImportant Functionsrio_writen(int fd, void *buf, size_t n) Writes n bytes from buffer buf to file fd.rio_readlineb(rio_t *rp, void *buf, size_t maxn) Read complete text line from file rp into buffer buf. » Line must be terminated by newline (\n) character Up to maximum of maxn bytesUsed Here For Illustrative Purposes Only You will need to check error returns Reading a whole line won’t always make sense (more later)1415-441, Spring 2004Echo Client Main Routine#include "csapp.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);Rio_readinitb(&rio, clientfd); while (Fgets(buf, MAXLINE, stdin) !=
View Full Document