Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.

Similar presentations


Presentation on theme: "The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between."— Presentation transcript:

1 The Socket Interface Chapter 22

2 Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between applications and TCP/IP protocols Why did we wait so long to cover this? –The interface architecture is not standardized –It is not appropriate to tie the protocols to a particular API because no single interface architecture works well on all systems Interface details depend on the operating system This interface has become a de facto standard It forms the basis for Microsoft’s Windows Socket interface

3 UNIX I/O and Network I/O UNIX was designed in the late 1960’s as a timesharing system for single process computers –It is a process-oriented operating system in which each application program executes as a user level process –An application program interacts with the operating system by making system calls System calls act like procedure calls in that they take arguments and return values

4 UNIX I/O and Network I/O The UNIX I/O primitives follow the pattern: open-read-write-close –Open is called to specify and get permission from the file or device to be used A small integer file descriptor is returned for future reference –Read transfers data to the user process –Write transfers data form the user process to the file or device –Close informs the operating system that operations are complete

5 Adding Network I/O to UNIX More complex network protocols were added to BSD UNIX The protocol interface allows programmers to: –create server code that awaits connections passively –create client code that forms connections actively –allow sending application programs to specify the destination address with each datagram instead of binding destinations at the time they call open –use other protocol suites besides just TCP/IP i.e. a 32 bit address must be identified as an IP address

6 The Socket Abstraction A socket is a generalization of the UNIX file access mechanism that provides an endpoint for communication –Applications request the operating system to create a socket when one is needed –The system returns an integer that is used by the application to reference the socket Sockets can be created without binding to a specific destination address

7 The Socket Abstraction The application can choose to: –Supply a destination address each time it uses the socket when sending datagrams –Bind the destination address to the socket and avoid specifying the destination repeatedly as in a TCP connection When it makes sense, sockets perform like UNIX files or devices –Data is transferred using read and write

8 Creating a Socket The socket function uses three arguments and returns an integer result result = socket (pf, type, protocol) –pf specifies the protocol family TCP/IP, Xerox, Apple, and UNIX –type specifies type of communication reliable stream, connectionless datagram, raw type (allows access to low-level protocols or network interfaces) –protocol specifies one protocol if a family has multiple protocols that support one type of service i.e. a single family might have 2 connectionless deliveries

9 Creating a Socket The UNIX pipe mechanism –An interprocess communication mechanism that uses a reliable stream delivery service –The calling process creates both endpoints for communication simultaneously –Function socketpair creates two sockets simultaneously socketpair (pf, type, protocol, sarray) the two socket descriptors are place in sarray which is a two- element integer array

10 Socket Inheritance and Termination UNIX uses the fork and exec system calls to start new application programs –First, fork creates a copy of the currently executing application program This copy inherits access to all open sockets and it inherits access to all open files –Then exec is called so that the new copy can execute The operating system keeps a reference count associated with each socket, so it knows how many applications have access to it When a process finishes using a socket, it calls close –The reference count is decremented

11 Specifying a Local Address In many cases, sockets are created without care for which local address is assigned However, server processes at well-known ports must be able to specify that port –After a socket has been created, a server binds it to a local address bind (socket, localaddr, addrlen) socket is socket descriptor (integer) of the socket to be bound localaddr is a structure shown in Figure 22.1that specifies the local address to which the socket should be bound addrlen specifies length of the address in bytes

12 Specifying a Local Address –The structure sockaddr (in Figure 22.1) has: a 16-bit ADDRESS FAMILY that identifies the protocol suite to which the address belongs and determines what follows an address of up to 14 octets –The format of a socket address structure for a TCP/IP address is shown in Figure 22.2

13 Connecting Sockets to Destination Addresses Initially a socket is not associated with any foreign address –The function connect binds a permanent destination to a socket, placing it in a connected state connect (socket, destaddr, addrlen) destaddr is a socket address structure that specifies the destination address to which the socket should be bound –A connect must be made for reliable transport It is not necessary for connectionless datagram service, but may be used; the destination will not have to be specified with each datagram

14 Sending Data Through a Socket Once an application has established a socket, it uses the socket to transmit data –write - requires buffer and length –writev - requires a vector of pointers to blocks of data –send - has flags that could indicate out-of-band data –sendto and sendmsg - require a destination; sendmsg is used with a message structure send, write and writev only work with connected sockets, as they do not specify a destination address

15 Receiving Data Through a Socket An application may receive data through a socket –read - when a socket is connected read (descriptor, buffer, length) –readv - allows reading from noncontiguous locations –recv - uses flags similar to send –recvfrom - allows input from an unconnected socket recvfrom (socket, buffer, length, flags, fromaddr,len) –recvmsg - analogous to sendmsg with structure

16 Obtaining Local and Remote Socket Addresses Newly created processes inherit the set of open sockets from the process that created them –Sometimes the newly created process needs to determine the destination address to which a socket connects getpeername - determines the address of the peer (the remote end) to which a socket connects –getpeername ( socket, destaddr, addrlen) getsockname - returns the local address associated with a socket –getsockname ( socket, localaddr, addrlen)

17 Obtaining and Setting Socket Options In addition to binding a socket to a local address or connecting it to a destination address, we need a way for the application to control the socket –get or set timeout parameters –control allocation of buffer space –control processing of out-of-band data Instead of providing different commands for each option, two operations are given: –getsockopt - application requests information of socket –setsockopt - sets an option based on values from getsockopt

18 Specifying a Queue Length for a Server Typically, a server –creates a socket –binds it to a well-known port –listens for requests A new request may arrive before the server has finished with an old request –The server uses listen to put the socket in a passive mode ready to accept simultaneous requests at the socket (only for sockets that have reliable stream) listen (socket, qlength)

19 How a Server Accepts Connections A server uses socket, bind and listen commands –to create a socket, bind it to a well-known port, and specify queue length for listening At this point the socket is not connected to a specific foreign destination –the server waits for a connection, using the accept function newsock = accept (socket, addr, addrlen) –when a connection request arrives, the system fills in the addr (a pointer to a structure of type sockaddr) –the system creates a new socket that has its destination connected to the requesting client

20 How a Server Accepts Connections The server can handle connection requests –iteratively - server handles the request itself, closes the new socket and calls accept to get the next connection request –concurrently - server creates a slave which inherits a copy of the new socket, the slave services the request and closes the socket Multiple processes use the same local port, but they must connect to different destinations –When a TCP segment arrives, it is sent to the socket connected to the segment’s source In a concurrent server there is one process per client and one process that accepts connections –The socket used by the master server has a wildcard for the foreign destination

21 Other System Calls select - allows a single process to wait for connections on mulitple sockets gethostname - allows user processes to access the host name sethostname - allows privileged processes to set the host name setdomainname - notifies the O.S. on each host as to which domain it is in getdomainname - allows processes to retrieve the domain name

22 Other System Calls gethostbyname - takes a domain name and returns a pointer to a structure of information for that host gethostbyaddr - same as above, except it accepts a host address as an argument getnetbyname - obtains and formats database entry given the domain name of a network getnetbyaddr - to get information about a network given its address getservbyname - maps a named service onto a port number getservbyport - allows the caller to obtain an entry from the services database given the port number assigned to it

23 Socket Library Calls System calls pass control to the O.S. Library routines are bound into an application program as shown in Figure 22.5

24 Network Byte Order Conversion Routines Conversion routines for converting between the local machine byte order and the network standard byte order –ntohs - network order to local host order (short 2-bytes) –ntohl - network order to local host order (long 4-bytes) –htons - local host order to network order (short 2-bytes) –htonl - local host order to network order (long 4-bytes)

25 An Example Client Implementation of whois client –Allows a client on one machine to get information about a user on a remote system –The client is an application program that a user invokes with two arguments the name of a remote machine the name of a user on that machine –The client calls gethostbyname to map the remote machine name into an IP address getservbyname to find the well-known port for the whois service –The client creates a socket and binds it to the whois protocol port (43) on the destination machine

26 An Example Server Listens on the whois port Returns requested information in response to a request from any client Information is taken from the UNIX password file on the server’s machine

27 Summary The interface between an application program and TCP/IP protocols depends on the O.S. To use TCP, a program creates a socket, binds addresses to it, accepts incoming connections, communicates with reads and writes, and closes the socket System calls and library routines help programmers

28 For Next Time Read Chapter 23 Class presentations start with Chapter 24


Download ppt "The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between."

Similar presentations


Ads by Google