Network Programming Jan 13, 2005A Client-Server ExchangeNetwork ApplicationsInternet Connections (TCP/IP)ClientsUsing Ports to Identify ServicesServersOverview of the Sockets InterfaceSocketsSocket Programming ClichesSocket Programming HelpSocket Address StructuresReliable I/O (RIO) SummaryEcho Client Main RoutineEcho Client: open_clientfdEcho Client: open_clientfd (socket)Echo Client: open_clientfd (gethostbyname)Echo Client: open_clientfd (connect)Servers and sockets – 1 isn’t enoughConnected vs. Listening DescriptorsEcho Server: accept IllustratedEcho Server: Main LoopEcho Server: open_listenfdEcho Server: open_listenfd (cont)Echo Server: open_listenfd (socket)Echo Server: open_listenfd (initialize socket address)Echo Server: open_listenfd (bind)Echo Server: open_listenfd (listen)Echo Server: acceptEcho Server: Main RoutineEcho Server: Identifying the ClientEcho Server: echoRunning Echo Client/ServerIterative ServersFundamental Flaw of Iterative ServersConcurrent ServersPossible Mechanisms for Creating Concurrent FlowsEvent-Based Concurrent Servers Using I/O MultiplexingThe select FunctionMacros for Manipulating Set DescriptorsEvent-based Concurrent Echo ServerEvent-based Concurrent Server (cont)Slide 43Slide 44Slide 45Pro and Cons of Event-Based DesignsAttack #1Attack #2: Partial LinesFlaky ClientImplementing a Robust ServerRobust ServerRobust Server LoopConceptual ModelFor More InformationSlide 55GDB and RCSDebugging with gdbControlling Your Program With gdbExamining the State of Your ProgramVersion Control with RCSCreating RCS FilesChecking out filesVersionsMore information…Network ProgrammingJan 13, 2005TopicsProgrammer’s view of the InternetSockets interfaceWriting clients and serversConcurrency with I/O multiplexingDebugging With GDBVersion Control With RCSclass03.ppt15-441David A. MaltzSlides based on work by •Randy Bryant and the 15-213 crew•Geoff Langdale215-441, Spring 2005A Client-Server ExchangeClientprocessServerprocess1. Client sends request2. Server handlesrequest3. Server sends response4. Client handlesresponseResourceA 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).315-441, Spring 2005Network ApplicationsAccess to Network via Program InterfaceSockets make network I/O look like filesCall system functions to control and communicateNetwork code handles issues of routing, segmentation.OSNetworkInterfaceClientAppl.SocketOS +NetworkAPIsOSNetworkInterfaceServerAppl.SocketOS +NetworkAPIsInternetClient MachineClient MachineServer MachineServer Machine415-441, Spring 2005Internet 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.15Two common paradigms for clients and servers communicationDatagrams (UDP protocol SOCK_DGRAM)Connections (TCP protocol, SOCK_STREAM) Connections are point-to-point, full-duplex (2-way communication), and reliable. (TODAY’S TOPIC!)Note: 3479 is anephemeral port allocatedby the kernel Note: 80 is a well-known portassociated with Web servers515-441, Spring 2005ClientsExamples of client programsWeb 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 portsPort 7: Echo serverPort 23: Telnet serverPort 25: Mail serverPort 80: Web server615-441, Spring 2005Using 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)KernelKernelClientClient715-441, Spring 2005ServersServers 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 serverPort 23: telnet serverPort 25: mail serverPort 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 2005Client / ServerSessionOverview of the Sockets InterfaceClientServersocket socketbindlistenreadwritereadwriteConnectionrequestreadclosecloseEOFAwait connectionrequest fromnext clientopen_listenfdopen_clientfdacceptconnect915-441, Spring 2005SocketsWhat 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.1015-441, Spring 2005Socket Programming ClichesNetwork Byte OrderingNetwork is big-endian, host may be big- or little-endianFunctions work on 16-bit (short) and 32-bit (long) values htons() / htonl() : convert host byte order to network byte orderntohs() / ntohl(): convert network byte order to host byte orderUse these to convert network addresses, ports, …Structure CastsYou 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)); …1115-441, Spring 2005Socket Programming Helpman is your friend (aka RTFM)man acceptman selectEtc. The manual page will tell you:What #include<> directives you need at the top of your source codeThe type of each argumentThe possible return valuesThe possible errors (in errno)1215-441, Spring 2005Socket Address StructuresGeneric socket address:For
View Full Document