Computer Networks and Internets with Internet Applications 4

  • Slides: 17
Download presentation
Computer Networks and Internets with Internet Applications, 4 e By Douglas E. Comer Lecture

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

Chapter 29 The Socket Interface © 2007 Pearson Education Inc. , Upper Saddle River,

Chapter 29 The Socket Interface © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

Topics Covered • • 29. 1 Introduction 29. 2 Application Program Interface 29. 3

Topics Covered • • 29. 1 Introduction 29. 2 Application Program Interface 29. 3 The Socket API 29. 4 Sockets And Socket Libraries 29. 5 Socket Communication And UNIX I/O 29. 6 Sockets, Descriptors, And Network I/O 29. 7 Parameters And The Socket API © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

Topics Covered (cont. ) • 29. 8 Procedures That Implement The Socket API –

Topics Covered (cont. ) • 29. 8 Procedures That Implement The Socket API – – – – 29. 8. 1 The Socket Procedure 29. 8. 2 The Close Procedure 29. 8. 3 The Bind Procedure 29. 8. 4 The Listen Procedure 29. 8. 5 The Accept Procedure 29. 8. 6 The Connect Procedure 29. 8. 7 The Send, Sendto, And Sendmsg Procedures 29. 8. 8 The Recv, Recvfrom, And Recvmsg Procedures • 29. 9 Read And Write With Sockets • 29. 10 Other Socket Procedures • 29. 11 Sockets, Threads, And Inheritance © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 1 Introduction The chapter • presents the motivation for using client-server • discusses

29. 1 Introduction The chapter • presents the motivation for using client-server • discusses concepts such as concurrency • shows how clients/servers use transport protocols to communicate • provides additional details about client-server interaction by explaining the interface between an application and protocol SW • considers how an application uses protocol SW to communicate • explains an example set of procedures – that an application uses to become a client or a server, – to contact a remote destination, – or to transfer data © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 2 Application Program Interface • An application must specify some details for communication

29. 2 Application Program Interface • An application must specify some details for communication • An application uses SW tools is known as an – Application Program Interface (API) • An API defines a set of operations that an application can perform when interacting with protocol SW. • Most programming systems define an API – by listing a set of procedures available to applications – the arguments that each procedure expects – and the data types • Usually, an API contains a separate procedure for each logical function © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 3 The Socket API • Communication protocols do not usually specify an API

29. 3 The Socket API • Communication protocols do not usually specify an API that applications use to interact with the protocols – Instead, the protocols specify the general operations that should be provided – and allow each OS to define the specific API an application uses to perform the operations • OS designers to choose an API, many have adopted the – “socket API” or simply “socket” • The socket API is available for many OS © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 4 Sockets And Socket Libraries (1) • From an application programmer's point of

29. 4 Sockets And Socket Libraries (1) • From an application programmer's point of view – a “socket library” provides the same semantics as an implementation of sockets in the OS – the program calls socket procedures, which are either supplied by OS procedures or library routines. • In practice, socket libraries are seldom perfect – and minor differences sometimes occur between the standard implementation of a socket API and a socket library © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 4 Sockets And Socket Libraries (2) • Despite apparent similarities, socket libraries have

29. 4 Sockets And Socket Libraries (2) • Despite apparent similarities, socket libraries have a completely different implementation than a native socket API supplied by an OS • The code for socket library procedures is linked into the application program and resides in the application's address space • When an application calls a procedure from the socket library – control passes to the library routine which, in turn, makes one or more calls to the underlying OS functions to achieve the desired effect • Functions supplied by the underlying OS need not resemble the socket API at all • Routines in the socket library hide the native OS from the application and present only a socket interface © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 5 Socket Communication And UNIX I/O • Sockets are integrated with I/O –

29. 5 Socket Communication And UNIX I/O • Sockets are integrated with I/O – an application communicates through a socket similar to the way the application transfers data to or from a file – understanding sockets requires one to understand UNIX I/O • UNIX uses an open-read-write-close paradigm for all I/O – the name is derived from the basic I/O operations that apply to both devices and files • For example, – an application must first call “open” to prepare a file for access – The application then calls “read” or “write” to retrieve data from the file or store data in the file – Finally, the application calls “close” to specify that it has finished using the file © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 6 Sockets, Descriptors, And Network I/O (1) • Before an application can use

29. 6 Sockets, Descriptors, And Network I/O (1) • Before an application can use protocols to communicate – The application must request the OS to create a socket that will be used for communication – The system returns a small integer “descriptor” that identifies the socket – The application then passes the descriptor as an argument when it calls procedures to transfer data across the NW • the application does not need to specify details about the remote destination each time it transfers data • The OS provides a single set of descriptors for files, devices, interprocess communication (IPC) • UNIX provides a pipe mechanism for IPC © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 6 Sockets, Descriptors, And Network I/O (2) • An application can use the

29. 6 Sockets, Descriptors, And Network I/O (2) • An application can use the same procedure to send data to – another program, a file, or across a NW • The descriptor represents an object, and the “write” procedure represents a method applied to that object – The underlying object determines how the method is applied • The chief advantage of an integrated system lies in its flexibility – a single application can be written that transfers data to an arbitrary location • If the application is given a descriptor that corresponds to a device – The application sends data to the device • If the application is given a descriptor that corresponds to a file – the application stores data in the file. • If the application is given a descriptor that corresponds to a socket – the application sends data across an internet to a remote machine © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 7 Parameters And The Socket API • Socket programming differs from conventional I/O

29. 7 Parameters And The Socket API • Socket programming differs from conventional I/O – because an application must specify many details to use a socket • For example, – an application must choose a particular transport protocol, – provide the protocol address of a remote machine, – and specify whether the application is a client or a server • To accommodate all the details – each socket has many parameters and options – an application can supply values for each • How should options and parameters be represented in an API? – To avoid having a single socket function with separate parameters for each option, designers of the socket API chose to define many functions. – An application creates a socket and then invokes functions to specify in detail • • The advantage is that most functions have few parameters The disadvantage is that, we must remember to call multiple functions © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 8 Procedures That Implement The Socket API Please refer to following sub-sections for

29. 8 Procedures That Implement The Socket API Please refer to following sub-sections for procedures being used and parameters being passed: – – – – 29. 8. 1 The Socket Procedure 29. 8. 2 The Close Procedure 29. 8. 3 The Bind Procedure 29. 8. 4 The Listen Procedure 29. 8. 5 The Accept Procedure 29. 8. 6 The Connect Procedure 29. 8. 7 The Send, Sendto, And Sendmsg Procedures 29. 8. 8 The Recv, Recvfrom, And Recvmsg Procedures © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 9 Read And Write With Sockets • Sockets allow applications to use read

29. 9 Read And Write With Sockets • Sockets allow applications to use read and write • Like send and recv , read and write do not have arguments that permit the caller to specify a destination • Instead, read and write each have three arguments: – a socket descriptor – the location of a buffer in memory used to store the data – and the length of the memory buffer • The chief advantage of using read and write is generality – an application program can be created that transfers data to or from a descriptor • without knowing whether the descriptor corresponds to a file or a socket • thus, a programmer can use a file on a local disk to test a client or server before attempting to communicate across a NW • The chief disadvantage is that a socket library implementation may introduce additional overhead in the file I/O © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 10 Other Socket Procedures • The socket API contains other useful procedures •

29. 10 Other Socket Procedures • The socket API contains other useful procedures • For example, – after a server calls procedure accept to accept an incoming connection request, the server can call procedure “getpeername” • to obtain the complete address of the remote client that initiated the connection – A client or server can also call “gethostname” • to obtain information about the computer on which it is running • Two general-purpose procedures are used to set socket options or obtain a list of current values, an application calls procedure – “setsockopt” to store values in socket options – “getsockopt” to obtain current option values – Options are used mainly to handle special cases © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.

29. 11 Sockets, Threads, And Inheritance • Because many servers are concurrent, the socket

29. 11 Sockets, Threads, And Inheritance • Because many servers are concurrent, the socket API is designed to work with concurrent programs • Although the details depend on the underlying OS implementations of the socket API adhere to the following principle: – Each new thread that is created inherits a copy of all open sockets from the thread that created it © 2007 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.