Network Programming IntroductionJan. 25, 2006TopicsTopicsProgrammer's view of the InternetSockets interfaceWriting clients and serversConcurrency with I/O multiplexingL03a_Sockets15-441(Borrowing heavily from 15-213)– 2 –15-441, Spring 2006About This Lecture“Intro to writing client/server programs with TCP”“Intro to writing client/server programs with TCP”Stolen from 15-213Should be “review”Will zoom through these slidesYou may review at your leisureExtensions to reach Project 1Extensions to reach Project 115-213 “rio” package may not be advisableYou'll use UDP, not TCPPacket protocol rather than byte-streamNo “connections” (hence no “disconnections” aka EOFs)You may find error reporting confusing at first– 3 –15-441, Spring 2006A Client-Server TransactionClientprocessServerprocess1. Client sends request2. Server handlesrequest3. Server sends response4. Client handlesresponseResourceEvery network application is based on the client-server Every network application is based on the client-server model:model:A server process and one or more client processesServer 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).– 4 –15-441, Spring 2006Network ApplicationsAccess to Network via Program InterfaceAccess to Network via Program InterfaceSockets make network I/O look like filesCall system functions to control and communicateNetwork code handles issues of routing, reliability, ordering, &c.Client ComputerOSNetworkInterfaceClientAppl.SocketOS +NetworkAPIsServer ComputerOSNetworkInterfaceServerAppl.SocketOS +NetworkAPIsInternet– 5 –15-441, Spring 2006ClientsExamples of client programsExamples of client programsWeb browsers, ftp, telnet, sshHow does a client specify a server?How does a client specify a 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 portsPort 7: Echo serverPort 23: Telnet serverPort 25: Mail serverPort 80: Web server– 6 –15-441, Spring 2006Internet 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.242 Server host address208.216.181.15Clients and servers communicate by sending streams Clients and servers communicate by sending streams of bytes over of bytes over connectionsconnections..Connections are point-to-point, full-duplex (2-way Connections are point-to-point, full-duplex (2-way communication), and reliable.communication), and reliable.Note: 3479 is anephemeral port allocatedby the kernel Note: 80 is a well-known portassociated with Web servers– 7 –15-441, Spring 2006Using 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)KernelKernelClientClient– 8 –15-441, Spring 2006ServersServers are long-running processes (daemons).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 Each server waits for requests to arrive on a well-known port associated with a particular service.port associated with a particular service.Port 7: echo serverPort 23: telnet serverPort 25: mail serverPort 80: HTTP serverA machine that runs a server process is also often A machine that runs a server process is also often referred to as a “ server.”referred to as a “ server.”See /etc/services for a list of semi-standard service to port bindings.– 9 –15-441, Spring 2006Sockets InterfaceCreated in the early 80’s as part of the original Berkeley Created in the early 80’s as part of the original Berkeley distribution of Unix that contained an early version of distribution of Unix that contained an early version of the Internet protocols.the Internet protocols.Provides a user-level interface to the network.Provides a user-level interface to the network.Underlying basis for all Internet applications.Underlying basis for all Internet applications.Based on client/server programming model.Based on client/server programming model.– 10 –15-441, Spring 2006Client / ServerSessionOverview of the Sockets InterfaceClientServersocket socketbindlistenrio_readlinebrio_writenrio_readlinebrio_writenConnectionrequestrio_readlinebclosecloseEOFAwait connectionrequest fromnext clientopen_listenfdopen_clientfdacceptconnect– 11 –15-441, Spring 2006SocketsWhat is a socket?What 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 Clients and servers communicate with each by reading from and writing to socket descriptors.from and writing to socket descriptors.The main distinction between regular file I/O and socket The main distinction between regular file I/O and socket I/O is how the application “ opens” the socket I/O is how the application “ opens” the socket descriptors.descriptors.– 12 –15-441, Spring 2006Socket Address StructuresGeneric socket address: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: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
View Full Document