1Introduction to Unix NetworkProgrammingReference: Stevens UnixNetwork Programming8/29/07 UIUC - CS/ECE 438, Fall 2007 2How do we Communicate? Send a mail from Alice to Bob Alice in Champaign, Bob in Hollywood Example: US Postal ServiceBobChampaign, IllinoisHollywood, CaliforniaAlice8/29/07 UIUC - CS/ECE 438, Fall 2007 3What does Alice do? Bob’s address (to a mailbox) Bob’s name – in case people share mailbox Postage – have to pay! Alice’s own name and address – in case Bob wants to return amessageBob100 Santa Monica Blvd.Hollywood, CA 90028Alice200 Cornfield Rd.Champaign, IL 618208/29/07 UIUC - CS/ECE 438, Fall 2007 4What does Bob do? Install a mailbox Receive the mail Get rid of envelope Read the messageBob100 Santa Monica Blvd.Hollywood, CA 90028Alice200 Cornfield Rd.Champaign, IL 618208/29/07 UIUC - CS/ECE 438, Fall 2007 5What about sending a TCP/IPpacket? Very similar to Alice-mailing-to-Bob Different terminologies – veryconfusing We have to remember Different technologies Suppose to be better (faster, morereliable, cheaper, …)8/29/07 UIUC - CS/ECE 438, Fall 2007 6Direction and PrinciplesPhysicalTransportData LinkNetworkProgramminglearn to use Internet forcommunication (with focus onimplementation of networkingconcepts)Principles andConceptslearn to build network from groundup28/29/07 UIUC - CS/ECE 438, Fall 2007 7SocketsprocessTCP withbuffers,variablessockethost orserverprocessTCP withbuffers,variablessockethost orserverInternetcontrolled byapp developer process sends/receivesmessages to/from itssocket socket analogous tomailbox sending process relieson transportinfrastructure whichbrings message tosocket at receivingprocess8/29/07 UIUC - CS/ECE 438, Fall 2007 8Network Programming withSockets Reading: Stevens 2nd or 3rd ed., Ch. 1-6 or 1st ed., Ch.1-3, 6 Sockets API: A transport layer service interface Introduced in 1981 by BSD 4.1 Implemented as library and/or system calls Similar interfaces to TCP and UDP Can also serve as interface to IP (for super-user);known as “raw sockets”8/29/07 UIUC - CS/ECE 438, Fall 2007 9Client-Server Model Asymmetric Communication Client sends requests Server sends replies Server/Daemon Well-known name (e.g., IP address +port) Waits for contact Processes requests, sends replies Client Initiates contact Waits for responseClientServerClientClientClient8/29/07 UIUC - CS/ECE 438, Fall 2007 10Client-Server CommunicationModel Service Model Concurrent: Server processes multiple clients’ requestssimultaneously Sequential: Server processes only one client’s requests at a time Hybrid: Server maintains multiple connections, but processesresponses sequentially Client and server categories are not disjoint A server can be a client of another server A server can be a client of its own client8/29/07 UIUC - CS/ECE 438, Fall 2007 11int main(int argc, char **argv) { int sockfd = socket(PF_INET, SOCK_STREAM, 0); struct hostent * he = gethostbyname(argv[1]); struct sockaddr_in their_addr; their_addr.sin_addr = *((struct in_addr *)he->h_addr); their_addr.sin_port = htons(atoi(argv[2])); their_addr.sin_family = AF_INET; memset(their_addr.sin_zero, ‘\0’, sizeof their_addr.sin_zero); connect(sockfd, (struct sockaddr *) their_addr, sizeof their_addr); write(sockfd, “GET /\r\n”, strlen(“GET /\r\n”)); while (1) { char buf[100]; int len = read(sockfd, buf, 100); if (len <= 0) { close(sockfd);return 0; } else { write(1, buf, len); } }}8/29/07 UIUC - CS/ECE 438, Fall 2007 12Sockets int sockfd = socket(PF_INET, SOCK_STREAM, 0); Creates a new socket SOCK_STREAM = TCP SOCK_DGRAM = UDP38/29/07 UIUC - CS/ECE 438, Fall 2007 13TCP Connections Transmission Control Protocol (TCP)Service OSI Transport Layer Service Model Reliable byte stream (interpreted by application) 16-bit port space allows multiple connections on asingle host Connection-oriented Set up connection before communicating Tear down connection when done8/29/07 UIUC - CS/ECE 438, Fall 2007 14TCP Service Reliable Data Transfer Guarantees delivery of all data Exactly once if no catastrophic failures Sequenced Data Transfer Guarantees in-order delivery of data If A sends M1 followed by M2 to B, B never receives M2before M1 Regulated Data Flow Monitors network and adjusts transmission appropriately Prevents senders from wasting bandwidth Reduces global congestion problems Data Transmission Full-Duplex byte stream8/29/07 UIUC - CS/ECE 438, Fall 2007 15UDP Services User Datagram Protocol Service OSI Transport Layer Provides a thin layer over IP 16-bit port space (distinct from TCPports) allows multiple recipients on asingle host8/29/07 UIUC - CS/ECE 438, Fall 2007 16UDP Services Unit of Transfer Datagram (variable length packet) Unreliable No guaranteed delivery Drops packets silently Unordered No guarantee of maintained order of delivery Unlimited Transmission No flow control8/29/07 UIUC - CS/ECE 438, Fall 2007 17Addressesstruct hostent * he = gethostbyname(argv[1]);struct sockaddr_in their_addr; their_addr.sin_addr = *((struct in_addr *)he->h_addr); Look up destination host name8/29/07 UIUC - CS/ECE 438, Fall 2007 18Addresses and Data Internet domain names Human readable Variable length Ex: sal.cs.uiuc.edu IP addresses Easily handled by routers/computers Fixed length Somewhat geographical Ex: 128.174.252.21748/29/07 UIUC - CS/ECE 438, Fall 2007 19Address Access/ConversionFunctions All binary values are network byte orderedstruct hostent* gethostbyname (const char*hostname); Translate English host name to IP address (uses DNS)struct hostent* gethostbyaddr (const char*addr, size_t len, int family); Translate IP address to English host name (not secure)char* inet_ntoa (struct in_addr inaddr); Translate IP address to ASCII dotted-decimal notation (e.g.,“128.32.36.37”)8/29/07 UIUC - CS/ECE 438, Fall 2007 20Structure: hostent The hostent data structure (from/usr/include/netdb.h) canonical domain name and aliases list of addresses associated with machine also address type and length informationstruct hostent {char* h_name; /* official name of host */char** h_aliases; /* NULL-terminated alias list */int h_addrtype /* address type (AF_INET) */int
View Full Document