1Introduction to Unix NetworkProgrammingReference: Stevens UnixNetwork Programming8/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 2How do we Communicate? Send a mail from Alice to Bob Alice in Champaign, Bob in Hollywood Example: US Postal ServiceBobChampaign, IllinoisHollywood, CaliforniaAlice8/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 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/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 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/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 5What about sending a TCP/IPpacket? Very similar to Alice-mailing-to-Bob Different terminologies Different technologies Suppose to be better (faster, morereliable, cheaper, …)8/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 6Two simplest networkingprograms Alice the sending process Alice’s address:128.174.246.177 (IP addr) Alice’s name: 12345 (port #) Bob the receiving process Bob’s address:216.52.167.132 (IP addr) Bob’s name: 23456 (port #)int main () { int sockfd; struct sockaddr_in bob_addr, alice_addr; bzero(&bob_addr, s izeof(bob_addr)); bob_addr.sin_fam ily = AF_INET; bob_addr.sin_a ddr.s _addr = 0xD834A784; bob_addr.sin_port = 23456; // do the sam e for alice_addr … sockfd = socket(AF_INET, SOCK_DGRAM, 0); sendto(s ockfd, “hi”, strlen(“hi”), 0, &bob_addr, s izeof(bob_addr));}int main () { int sockfd, n; struct sockaddr_in bob_addr, alice_addr; char mesg[100]; bzero(&bob_addr, s izeof(bob_addr)); bob_addr.sin_fam ily = AF_INET; bob_addr.sin_a ddr.s _addr = 0xD834A784; bob_addr.sin_port = 23456; sockfd = socket(AF_INET, SOCK_DGRAM, 0); bind(s ockfd, &bob_addr, sizeof(bob_addr)); n= recvfrom(s ockfd, mesg, 100, 0, &alice_addr, sizeof(alice_a ddr));}28/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 7What are the problems? Message may be lost Network is congested Receiver is congested Message may be duplicated, corrupted Multiple messages: re-ordered Concurrent connections …8/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 8Direction and PrinciplesPhysicalTransportData LinkNetworkProgramminglearn to use Internet forcommunication (with focus onimplementation of networkingconcepts)Principles andConceptslearn to build network from groundup8/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 9SocketsprocessTCP 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/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 10Network Programming withSockets Reading: Stevens 2nd 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/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 11Outline Client-Sever Model TCP/UDP Overview Addresses and Data Sockets API Example8/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 12Client-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 responseClientServerClientClientClient38/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 13Socket CommunicationClient processServer processClientsocket3-wayhands hakingListeningsocketConnectionsocket8/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 14Client-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/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 15TCP 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/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 16TCP 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/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 17UDP 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/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 18UDP 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 control48/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 19Addresses 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.2178/3 0/06 UIUC - CS/ECE 43 8, Fall 20 06 20Byte Ordering Big Endian vs. Little Endian Little Endian (Intel, DEC): Least significant byte of word is stored in the lowestmemory address Big Endian (Sun, SGI, HP): Most significant byte of word is stored in the lowestmemory address Network Byte Order = Big Endian Allows both sides to
View Full Document