DOC PREVIEW
CMU 15441 Computer Networking - Network Programming

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

CMU 15441 Computer Networking - Network Programming

Documents in this Course
Lecture

Lecture

14 pages

Lecture

Lecture

19 pages

Lecture

Lecture

14 pages

Lecture

Lecture

78 pages

Lecture

Lecture

35 pages

Lecture

Lecture

4 pages

Lecture

Lecture

4 pages

Lecture

Lecture

29 pages

Lecture

Lecture

52 pages

Lecture

Lecture

40 pages

Lecture

Lecture

44 pages

Lecture

Lecture

41 pages

Lecture

Lecture

38 pages

Lecture

Lecture

40 pages

Lecture

Lecture

13 pages

Lecture

Lecture

47 pages

Lecture

Lecture

49 pages

Lecture

Lecture

7 pages

Lecture

Lecture

18 pages

Lecture

Lecture

15 pages

Lecture

Lecture

74 pages

Lecture

Lecture

35 pages

Lecture

Lecture

17 pages

lecture

lecture

13 pages

Lecture

Lecture

21 pages

Lecture

Lecture

14 pages

Lecture

Lecture

53 pages

Lecture

Lecture

52 pages

Lecture

Lecture

40 pages

Lecture

Lecture

11 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Lecture

Lecture

10 pages

Lecture

Lecture

40 pages

Lecture

Lecture

25 pages

lecture

lecture

11 pages

lecture

lecture

7 pages

Lecture

Lecture

10 pages

lecture

lecture

46 pages

lecture

lecture

7 pages

Lecture

Lecture

8 pages

lecture

lecture

55 pages

lecture

lecture

45 pages

lecture

lecture

47 pages

lecture

lecture

39 pages

lecture

lecture

33 pages

lecture

lecture

38 pages

lecture

lecture

9 pages

midterm

midterm

16 pages

Lecture

Lecture

39 pages

Lecture

Lecture

14 pages

Lecture

Lecture

46 pages

Lecture

Lecture

8 pages

Lecture

Lecture

40 pages

Lecture

Lecture

11 pages

Lecture

Lecture

41 pages

Lecture

Lecture

38 pages

Lecture

Lecture

9 pages

Lab

Lab

3 pages

Lecture

Lecture

53 pages

Lecture

Lecture

51 pages

Lecture

Lecture

38 pages

Lecture

Lecture

42 pages

Lecture

Lecture

49 pages

Lecture

Lecture

63 pages

Lecture

Lecture

7 pages

Lecture

Lecture

51 pages

Lecture

Lecture

35 pages

Lecture

Lecture

29 pages

Lecture

Lecture

65 pages

Lecture

Lecture

47 pages

Lecture

Lecture

41 pages

Lecture

Lecture

41 pages

Lecture

Lecture

32 pages

Lecture

Lecture

35 pages

Lecture

Lecture

15 pages

Lecture

Lecture

52 pages

Lecture

Lecture

16 pages

Lecture

Lecture

4 pages

lecture

lecture

27 pages

lecture04

lecture04

46 pages

Lecture

Lecture

46 pages

Lecture

Lecture

13 pages

lecture

lecture

41 pages

lecture

lecture

38 pages

Lecture

Lecture

40 pages

Lecture

Lecture

25 pages

Lecture

Lecture

38 pages

lecture

lecture

11 pages

Lecture

Lecture

42 pages

Lecture

Lecture

12 pages

Lecture

Lecture

36 pages

Lecture

Lecture

46 pages

Lecture

Lecture

35 pages

Lecture

Lecture

34 pages

Lecture

Lecture

9 pages

lecture

lecture

49 pages

class03

class03

39 pages

Lecture

Lecture

8 pages

Lecture 8

Lecture 8

42 pages

Lecture

Lecture

20 pages

lecture

lecture

29 pages

Lecture

Lecture

9 pages

lecture

lecture

46 pages

Lecture

Lecture

12 pages

Lecture

Lecture

24 pages

Lecture

Lecture

41 pages

Lecture

Lecture

37 pages

lecture

lecture

59 pages

Lecture

Lecture

47 pages

Lecture

Lecture

34 pages

Lecture

Lecture

38 pages

Lecture

Lecture

28 pages

Exam

Exam

17 pages

Lecture

Lecture

21 pages

Lecture

Lecture

15 pages

Lecture

Lecture

9 pages

Project

Project

20 pages

Lecture

Lecture

40 pages

L13b_Exam

L13b_Exam

17 pages

Lecture

Lecture

48 pages

Lecture

Lecture

10 pages

Lecture

Lecture

52 pages

21-p2p

21-p2p

16 pages

lecture

lecture

77 pages

Lecture

Lecture

18 pages

Lecture

Lecture

62 pages

Lecture

Lecture

25 pages

Lecture

Lecture

24 pages

Project

Project

20 pages

Lecture

Lecture

47 pages

Lecture

Lecture

38 pages

Lecture

Lecture

35 pages

Roundup

Roundup

45 pages

Lecture

Lecture

47 pages

Lecture

Lecture

39 pages

Lecture

Lecture

13 pages

Midterm

Midterm

22 pages

Project

Project

26 pages

Lecture

Lecture

11 pages

Project

Project

27 pages

Lecture

Lecture

10 pages

Lecture

Lecture

50 pages

Lab

Lab

9 pages

Lecture

Lecture

30 pages

Lecture

Lecture

6 pages

r05-ruby

r05-ruby

27 pages

Lecture

Lecture

8 pages

Lecture

Lecture

28 pages

Lecture

Lecture

30 pages

Project

Project

13 pages

Lecture

Lecture

11 pages

Lecture

Lecture

12 pages

Lecture

Lecture

48 pages

Lecture

Lecture

55 pages

Lecture

Lecture

36 pages

Lecture

Lecture

17 pages

Load more
Download Network Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Network Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Network Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?