`
`.; 4 i{1 |
`
`Minn
`ito:
`
`
`Computer
`Networking
`
`ATop-Down Approach
`
`James F. Kurose
`
`University ofMassachusetts, Amherst
`
`Keith W. Ross
`
`Polytechnic University, Brooklyn
`
`
` AM aelee
`Teak|PNACOiB 22
`
`
`Alea
`
`Boston San Francisco New York
`London Toronto Sydney Tokyo Singapore Madrid
`Mexico City Munich Paris Cape Town Hong Kong Montreal
`
`
`
`Sportradar 1022
`Page 1
`
`
`
`
`
`Publisher
`Executive Editor
`Assistant Editor
`Associate Managing Editor
`Senior Production Supervisor
`Senior Marketing Manager
`Marketing Assistant
`Cover Designer
`Cover Image
`
`Greg Tobin
`Michael Hirsch
`Lindsey Triebel
`Jeffrey Holcomb
`Marilyn Lloyd
`Michelle Brown
`Sarah Milmore
`Joyce Cosentino Wells
`©Robert Harding/Getty Images: Bridge over
`DordogneRiver, Aquitaine, France
`Janet Theurer/Theurer Briggs Design
`Art Director
`Patrice Rossi Calkin/RossiIllustration & Design
`Art Studio
`Carol Melville
`Senior Manufacturing Buyer
`er
`Bethany Tidd
`Senior Media Producer
`NancyKotary, Alicia Williams, Scott Harris/Argosy Publishing, Inc.
`Project Management
`Argosy Publishing, Inc.
`Composition
`Manyofthe designations used by manufacturers and sellers to distinguishtheir products are
`claimed as trademarks. Wherethose designations appear in this book, and Addison-Wesley
`was aware of a trademarkclaim, the designations have beenprintedin initial caps orall caps.
`
`The programsandapplications presented in this book have beenincludedfor their instruc-
`tional value. They have beentested with care, but are not guaranteed for any particular pur-
`pose. The publisher doesnot offer any warranties or representations, nor doesit accept any
`liabilities with respect to the programsorapplications.
`
`Library of Congress Cataloging-in-Publication Data
`Kurose, JamesF.
`Computer networking : a top-down approach/ James F. Kurose,
`Keith W. Ross. — 4th ed.
`
`p. cm.
`ISBN 0-321-49770-8
`1. Internet. 2. Computer networks.I. Ross, Keith W., 1956- II. Title.
`TKS5105.875.I157K88 2007
`004.6—dc22
`
`2007002094
`
`.
`Copyright © 2008 Pearson Education,Inc.
`All rights reserved. No part of this publication may be reproduced,storedinaretrieval system,
`or transmitted, in any form or by any means,electronic, mechanical, photocopying, recording,
`or otherwise, withoutthe prior written permissionof the publisher. Printed in the United States
`of America. For information on obtaining permission for use of material in this work,please
`submit a written request to Pearson Education, Inc., Rights and Contracts Department,
`75 Arlington Street, Suite 300, Boston, MA 02116, fax your request to 617-848-7047, or
`e-mail at http://www.pearsoned.com/ legal/permissions.htm.
`
`ISBN-13: 978-0-321-49770-3
`ISBN-10: 0-321-49770-8
`
`3456789 10—CRW—10 09 08
`
`
`
`Sportradar 1022
`Page 2
`
`Sportradar 1022
`Page 2
`
`
`
`
`
`2.) PRINCIPLES OF HEDV/O2K APPLICATIONS©87
`
`The Interface Between the Process and the Computer Network
`beeea pairs ofcommunicating processes, with
`& Messages to cach other. Any message sent from
`:
`*
`one process to another must go through the underlying network. A process sends
`messages into, and receTes Messages from, the network througha software interface
`called a socket, Let’s consider an analogyto help us understand processes and sock-
`ets. A process1s analogousto a houseandits socketis analogousto its door. When a
`process wants to send a messageto anotherprocess on anotherhost, it shoves the mes-
`sage out its door(socket). This sending process assumesthatthere is a transportation
`infrastructure on the otherside ofits door thatwill transport the messageto the door
`of the destination process. Once the messagearrivesat the destination host, the mes-
`sage passes throughthereceiving process’s door(socket), and the receiving process
`then acts on the message.
`Figure 2.3 illustrates socket communication between two processes that com-
`municate over the Internet. (Figure 2.3 assumesthat the underlying transport proto-
`col used by the processesis the Internet’s TCP protocol.) As shownin this figure, a
`socketis the interface between the application layer and the transport layer within a
`host. It is also referred to as the Application Programming Interface (API)
`between the application and the network,since the socketis the programminginter-
`face with which network applications are built. The application developer has con-
`trol of everything on the application-layer side of the socket buthaslittle control of
`the transport-layer side of the socket. The only controlthat the application devel-
`oper has on the transport-layer side is (1) the choice of transport protocol and(2)
`perhapsthe ability to fix a few transport-layer parameters such as maximum buffer
`
`Host or
`server
`
`
`
`
`
`
`—
`
`
`Socket
` Controlled
`TCPwith
`Controlled----— }- ooh
`
`by operating
`
`by operating buffers, ——|buffers,
`
`system
`system
`variables
`variables
`Internet
`
`sockets, and underlying transport protocol
`
`Host or
`server
`
`by application
`developer
`
`by application
`developer
`
` Controlled ———-y
`
`Figure 2.3 Application processes,
`
`Sporiradar 1022
`Page 3
`
`a
`
`Sportradar 1022
`Page 3
`
`
`
`160
`
`CHAPTER 2
`
`° APPLICATION LAYER
`
`}
`
`2
`
`;
`.
`eit
`3
`single developer (or cic vai,
`necessarily conformto any existing os ind the developer ras, opment tea
`creates both the client and server programs, sd does not imple: 1 plete c nie
`ANCHE’
`‘
`sa11Se
`:
`over what goes in the code. But because the code do
`Pp
`PUblic-dy "ol
`&
`evelou Corde that;
`In
`;
`ore
`wi
`able to d
`protocol, other independent developers will not be é
`'
`™~Wat Nteroner,
`ates with the application. When developing a proprietary applicatic
`,
`elo
`ivi, the dey
`must be careful not to use one of the well-known port numberS definedin the REO”
`Inthis and the nextsection, we examinethe key issues in developing a Prope
`etaryclient-server application. During the development phase, oneofthefirst de,
`sions the developer must make is whether the application Is torUNOver TCPo, Ov
`UDP.Recall that TCPis connection oriented and provides a reliable byte-strea
`channel through whichdata flows between two end systems. UDPis CONNECtionles.
`and sendsindependentpackets ofdata from one end system to the other, Without any
`guarantees aboutdelivery.
`;
`In this section we develop a simple client application that runs over TCp. in the
`next section, we develop a simpleclient application that runs over UDP. We Present
`these simple TCP and UDPapplications in Java. We couldhave written the code th
`C or C++, but we opted for Java mostly because the applications are more neatly
`and cleanly written in Java. With Java there are fewerlines of code, and each line
`can be explained to the novice programmerwithout muchdifficulty. Buttherejs No
`needto befrightenedif you are not familiar with Java. You should be abletofollow
`the code if you have experience programmingin another language.
`Forreaders whoareinterestedin client/server programmingin C,thereare several]
`good referencesavailable [Donahoo 2001; Stevens 1997; Frost 1994; Kurose 1996].
`
`2.7.1 Socket Programming with TCP
`Recall from Section 2.1 that processes running on different machines communicate
`with each other by sending messagesinto sockets. We said that each process was
`analogousto a house andthe process’s socketis analogousto a door. As shownin
`Figure 2.30, the socket is the door between the application process and TCP.The
`application developerhas control of everything on the application-layerside ofthe
`socket; however,it has little control of the transport-layer side. (At the very most,
`the application developerhasthe ability to fix a few TCP parameters, such as max
`mum buffer size and maximum segmentsize.)
`Nowlet’s take a closer lookatthe interaction of the client and server program’
`The client hasthe job ofinitiating contact with the server. In order for the server[0
`be able to reactto the client’s initial contact, the server has to be ready. This implies
`two things.First, the server program cannot be dormant—that is, it must be runnine
`as a processbefore the client attemptsto initiate contact. Second,the server progra
`must have somesort of door—more precisely, a socket—that welcomes some in
`contact from a client process running on an arbitrary host. Using our houseltrr
`analogyfor a process/socket, we will sometimesrefer to the client’s initial con
`as “knocking on the welcoming door.”
`
`Sportradar 1022
`Page 4
`
`
`
`Sportradar 1022
`Page 4
`
`
`
`
`
`KEY pp
`
`mils WITH TCP
`Ay
`
`141
`
`Host or
`server
`
`controlled
`by application
`developer
`
`
`
`
`¢
`TCP with
`TCP with
`buffers, rs buffers,
`
`variables
`Internet
`
`
`
`
`
`Controlled
`by operating
`
`variables
`system
`
`Host or
`server
`(ar77]
`
`
`
`
`
`Controlled
`by application
`developer
`
`Controlled
`by operating
`system
`
`
`
`Figure 2.30 @ Processes communicating through TCP sockets
`
`With the server process running,the client process can initiate a TCP connec-
`tion to the server. This is done in the client program bycreating a socket. When the
`client creates its socket, it specifies the address of the server process, namely, the IP
`address of the server host and the port numberof the server process. Once the socket
`has been created in the client program, TCPin the clientinitiates a three-way hand-
`shake and establishes a TCP connectionwiththe server. The three-way handshake,
`which takes place at the transport layer, is completely transparentto the client and
`Server programs.
`During the three-way handshake,the client process knocks on the welcoming
`doorof the server process. When the server “hears” the knocking,it creates a new
`door—more precisely, a new socket—thatis dedicatedto thatparticularclient. In
`he example below, the welcoming door is a ServerSocketobjectthatwe call
`elopeomeSocket. Whena client knocks on this door, the program invokes
`tthe ees *s accept () method, which creates anew door for the client.
`Socket and ,the handshaking phase, a TCP connection exists neta the client’s
`bateg socker © Server’s new socket. Henceforth, werefer to the server’s new,dedi-
`aas theserver’s connection socket. Oo .
`.
`betwee
`the ae perspective, the TCP connection isa direct virtual pipe
`‘nd arbity cHent’s socket and the server’s connection socket, The client process can
`ary bytesinto its socket, and TCP guaranteesthat the server processwill
`eeive
`areli
`(through the connection socket) each byte in the order sent. TCP thus provides
`byte-stream service between the client and server processes. Furthermore,
`Just as
`Ple can 0 in and out the same door,the client processnotonly sends bytes
`
`e
`
`.
`
`:
`
`
`
`Sportradar 1022
`Page 5
`
`Sportradar 1022
`Page 5
`
`