Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets with Internet Applications, 4e By Douglas.

Similar presentations


Presentation on theme: "© 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets with Internet Applications, 4e By Douglas."— Presentation transcript:

1 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets with Internet Applications, 4e By Douglas E. Comer Lecture PowerPoints By Lami Kaya, LKaya@ieee.org

2 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.2 Chapter 30 Example Of A Client And A Server

3 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.3 Topics Covered 30.1 Introduction 30.2 Connection-Oriented Communication 30.3 An Example Service 30.4 Command-Line Arguments For The Example Programs 30.5 Sequence Of Socket Procedure Calls 30.6 Code For Example Client 30.7 Code For Example Server 30.8 Stream Service And Multiple Recv Calls 30.9 Socket Procedures And Blocking 30.10 Size Of The Code And Error Reporting 30.11 Using The Example Client With Another Service 30.12 Using Another Client To Test The Server

4 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.4 30.1 Introduction This chapter continues and expands the explanation of sockets by examining a client/server that use sockets to communicate it does not illustrate all possible client and server designs –But, the example code will clarify the purpose of major socket procedures it will show how socket programming differs from programs written using the API in Chapter 3 the example code illustrates a sequence of socket procedure calls it allows the reader to compare the sequence of calls used in a client and the sequence of calls used in a server

5 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.5 30.2 Connection-Oriented Communication A connection-oriented service requires two applications –to establish a transport connection before data can be sent To establish a connection –the applications each interact with transport protocol SW on their local computer, –and the two transport protocol modules exchange messages across the network After both sides agree that a connection has been established, the applications can send data

6 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.6 30.3 An Example Service To keep the size of the programs small and focus on the socket calls, we have chosen a trivial service: –the server keeps a count of the number of clients that have accessed the service –and reports the count whenever a client contacts the server To simplify implementation and debugging, we use ASCII A client forms a connection to a server and waits for output Whenever a connection request arrives, a server –creates a message in printable ASCII form –sends the message over the connection –and then closes the connection The client displays the data it receives and then exits

7 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.7 30.4 Command-Line Arguments For The Example Programs The example server takes one command-line (CLI) argument –a protocol port number at which to accept requests the argument is optional; if no port number is specified, the code uses port 5193 the particular port number used in the code has no significance The example client has two CLI arguments –the name of a host on which to contact a server and a protocol port number to use –Both arguments are optional If no protocol port number is specified, the client uses 5193 If neither argument is specified, the client uses the default port and the host name “localhost”

8 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.8 30.5 Sequence Of Socket Procedure Calls Figure 30.1 illustrates the sequence of socket procedures that the example client and server call As the figure shows –the server calls seven socket procedures –the client calls six socket procedures

9 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.9

10 10 30.6 Code For Example Client/Server The example code was compiled and tested –under BSD UNIX, Solaris, and Windows NT –with #ifdef constructs used to make minor changes UNIX Solaris Windows NT in the code (e.g., Windows Sockets uses closesocket instead of close ) client.c server.c

11 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.11 30.8 Stream Service And Multiple Recv Calls Server makes only one call to send to transmit data Client code iterates to receive data –during each iteration, the client calls recv to obtain data –the iteration stops when the client obtains an end-of-file condition In most cases, TCP on the server's computer will place the entire message in a single TCP segment, –and then transmit the segment across a TCP/IP internet in an IP datagram TCP does not guarantee that –the data will be sent in a single segment, –nor does it guarantee that each call to recv will return exactly the same amount of data that the server transferred in a call to send TCP merely asserts that –data will be delivered in order –With each call of recv returning one or more octets of data Consequently, a program that calls recv must be prepared to make repeated calls until all data has been extracted

12 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.12 30.9 Socket Procedures And Blocking Most procedures in the socket API are –“synchronous” or “blocking” That is, when a program calls a socket procedure –the program is suspended until the procedure completes –while suspended, a program does not use the CPU –no time limit for suspension, the operation may take arbitrarily long Calls to socket procedures in the client code can also block The call to connect blocks –until TCP can perform the 3-way handshake to establish a connection Perhaps the most important suspension occurs during data transmission

13 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.13 30.10 Size Of The Code And Error Reporting Although each of the example programs is large –much of the size can be attributed to comments –Exp: removing blank lines and comments reduces the code size Many lines of code check for errors In addition to checking values specified by CLI arguments –the code checks the return value from each procedure call to ensure that the operation was successful

14 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.14 30.11 Using The Example Client With Another Service Although the example service may seem trivial –both the client and server can be used with other services Using the example client with another service provides an easy way –to debug the client before the server has been written

15 © 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.15 30.12 Using Another Client To Test The Server The example server can be tested separately from the client To do so, one can use the “telnet” client program to contact the server The telnet program requires two arguments –the name of a computer the protocol port number of the server The following output shows the result of using telnet to connect to the example server  telnet xx.yy.nonexist.com 5193  Trying...  Connected to xx.yy.nonexist.com 5193


Download ppt "© 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets with Internet Applications, 4e By Douglas."

Similar presentations


Ads by Google