Introduction to Unix Network ProgrammingHow do we Communicate?What does Alice do?What does Bob do?What about sending a TCP/IP packet?Two simplest networking programsWhat are the problems?Direction and PrinciplesSocketsNetwork Programming with SocketsOutlineClient-Server ModelSocket CommunicationClient-Server Communication ModelTCP ConnectionsTCP ServiceUDP ServicesSlide 18Addresses and DataByte OrderingByte Ordering FunctionsSocket Address StructureAddress Access/Conversion FunctionsStructure: hostentSlide 25Sockets APIUDP Connection ExampleFunctions: sendtoFunctions: recvfromSocket FunctionsSlide 31TCP Connection ExampleSocket Creation and SetupFunctions: socketFunction: bindTCP and UDP PortsFunctions: listenFunctions: acceptserverSlide 40Slide 41Slide 42Establishing a ConnectionFunctions: connectclientSending and Receiving DataSlide 47Functions: writeFunctions: readTearing Down a ConnectionFunctions: closeFunctions: shutdownAdvanced SocketsExamplesSlide 55Slide 56Slide 57Slide 58Slide 59Slide 60Slide 61Slide 62Slide 63Introduction to Unix Network ProgrammingReference: Stevens Unix Network Programming8/30/06 UIUC - CS/ECE 438, Fall 2006 2How do we Communicate?Send a mail from Alice to BobAlice in Champaign, Bob in HollywoodExample:US Postal ServiceBobChampaign, IllinoisHollywood, CaliforniaAlice8/30/06 UIUC - CS/ECE 438, Fall 2006 3What does Alice do?Bob’s address (to a mailbox)Bob’s name – in case people share mailboxPostage – have to pay!Alice’s own name and address – in case Bob wants to return a messageBob100 Santa Monica Blvd.Hollywood, CA 90028Alice200 Cornfield Rd.Champaign, IL 618208/30/06 UIUC - CS/ECE 438, Fall 2006 4What does Bob do?Install a mailboxReceive the mailGet rid of envelopeRead the messageBob100 Santa Monica Blvd.Hollywood, CA 90028Alice200 Cornfield Rd.Champaign, IL 618208/30/06 UIUC - CS/ECE 438, Fall 2006 5What about sending a TCP/IP packet?Very similar to Alice-mailing-to-BobDifferent terminologies – very confusingWe have to rememberDifferent technologiesSuppose to be better (faster, more reliable, cheaper, …)8/30/06 UIUC - CS/ECE 438, Fall 2006 6Two simplest networking programsAlicethe sending processAlice’s address: 128.174.246.177 (IP addr)Alice’s name: 12345 (port #)Bobthe receiving processBob’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, sizeof(bob_addr)); bob_addr.sin_family = AF_INET; bob_addr.sin_addr.s_addr = 0xD834A784; bob_addr.sin_port = 23456; // do the same for alice_addr … sockfd = socket(AF_INET, SOCK_DGRAM, 0); sendto(sockfd, “hi”, strlen(“hi”), 0, &bob_addr, sizeof(bob_addr));}int main () { int sockfd, n; struct sockaddr_in bob_addr, alice_addr; char mesg[100]; bzero(&bob_addr, sizeof(bob_addr)); bob_addr.sin_family = AF_INET; bob_addr.sin_addr.s_addr = 0xD834A784; bob_addr.sin_port = 23456; sockfd = socket(AF_INET, SOCK_DGRAM, 0); bind(sockfd, &bob_addr, sizeof(bob_addr)); n= recvfrom(sockfd, mesg, 100, 0, &alice_addr, sizeof(alice_addr));}8/30/06 UIUC - CS/ECE 438, Fall 2006 7What are the problems?Message may be lostNetwork is congestedReceiver is congestedMessage may be duplicated, corruptedMultiple messages: re-orderedConcurrent connections…8/30/06 UIUC - CS/ECE 438, Fall 2006 8Direction and PrinciplesPhysicalTransportData LinkNetworkProgramminglearn to use Internet for communication (with focus on implementation of networking concepts)Principles and Conceptslearn to build network from ground up8/30/06 UIUC - CS/ECE 438, Fall 2006 9SocketsprocessTCP withbuffers,variablessockethost orserverprocessTCP withbuffers,variablessockethost orserverInternetcontrolled byapp developerprocess sends/receives messages to/from its socketsocket analogous to mailboxsending process relies on transport infrastructure which brings message to socket at receiving process8/30/06 UIUC - CS/ECE 438, Fall 2006 10Network Programming with SocketsReading: Stevens 2nd ed., Ch. 1-6 or 1st ed., Ch. 1-3, 6Sockets API: A transport layer service interfaceIntroduced in 1981 by BSD 4.1Implemented as library and/or system callsSimilar interfaces to TCP and UDPCan also serve as interface to IP (for super-user); known as “raw sockets”8/30/06 UIUC - CS/ECE 438, Fall 2006 11OutlineClient-Sever ModelTCP/UDP OverviewAddresses and DataSockets APIExample8/30/06 UIUC - CS/ECE 438, Fall 2006 12Client-Server ModelAsymmetric CommunicationClient sends requestsServer sends repliesServer/DaemonWell-known name (e.g., IP address + port)Waits for contactProcesses requests, sends repliesClientInitiates contactWaits for responseClientServerClientClientClient8/30/06 UIUC - CS/ECE 438, Fall 2006 13Socket CommunicationClient processServer processClientsocket3-wayhandshakingListeningsocketConnectionsocket8/30/06 UIUC - CS/ECE 438, Fall 2006 14Client-Server Communication ModelService ModelConcurrent:Server processes multiple clients’ requests simultaneouslySequential:Server processes only one client’s requests at a timeHybrid:Server maintains multiple connections, but processes responses sequentiallyClient and server categories are not disjointA server can be a client of another serverA server can be a client of its own client8/30/06 UIUC - CS/ECE 438, Fall 2006 15TCP ConnectionsTransmission Control Protocol (TCP) ServiceOSI Transport LayerService ModelReliable byte stream (interpreted by application)16-bit port space allows multiple connections on a single hostConnection-orientedSet up connection before communicatingTear down connection when done8/30/06 UIUC - CS/ECE 438, Fall 2006 16TCP ServiceReliable Data TransferGuarantees delivery of all dataExactly once if no catastrophic failures Sequenced Data TransferGuarantees in-order delivery of dataIf A sends M1 followed by M2 to B, B never receives M2 before M1Regulated Data FlowMonitors network and adjusts transmission appropriatelyPrevents senders from wasting bandwidthReduces global congestion problemsData TransmissionFull-Duplex byte stream8/30/06 UIUC - CS/ECE 438, Fall 2006 17UDP ServicesUser Datagram Protocol ServiceOSI Transport LayerProvides a thin layer over
View Full Document