The Socket Interface Chapter 21 Application Program Interface

  • Slides: 25
Download presentation
The Socket Interface Chapter 21

The Socket Interface Chapter 21

Application Program Interface (API) n n n Interface used between application programs and TCP/IP

Application Program Interface (API) n n n Interface used between application programs and TCP/IP protocols Will look at one example that is a de facto standard Things to keep in mind Application/protocol interaction not in the standards n Distinguish between interface and protocols In practice, details of interface depend on OS Example is from BSD UNIX operating system n n Is a widely accepted, de facto standard Operations listed are NOT part of the TCP/IP standards

UNIX I/O Paradigm Unix is a process oriented OS n n Applications execute as

UNIX I/O Paradigm Unix is a process oriented OS n n Applications execute as a user level process Application interacts with OS via system calls Act just like procedure calls I/O is open-read-write-close n n n Call open to get file descriptor file or device Call read or write to do I/O Call close to indicate done with object

Adding Network I/O to UNIX Originally, all I/O was open-read-write-close Needed to add network

Adding Network I/O to UNIX Originally, all I/O was open-read-write-close Needed to add network protocols to UNIX n Interactions more complex Passive server code as well as active client code Specify datagram address when send vs when open n n Abandoned the above paradigm Additions made I/O interface more complex Needed general mechanism for any protocol interface

Socket Abstraction Socket is basis for network I/O n n n Mechanism that provides

Socket Abstraction Socket is basis for network I/O n n n Mechanism that provides an endpoint for communication Create when needed; get integer to reference Application can choose address binding method TCP connection n Both endpoints must be specified Use for UDP n n Remote endpoint can be left unspecified Pass as argument each time message is sent

Creating a Socket Use socket function to create a socket result = socket(pf, type,

Creating a Socket Use socket function to create a socket result = socket(pf, type, protocol) n n pf specifies protocol family type specifies communication type Reliable stream; connectionless datagram; raw n protocol specifies a specific protocol in the family Need more than just family and type Ex: pipe in UNIX family cannot do packet delivery Programmer must know protocol family well

Socket Inheritance & Termination Two system calls used to start new application programs n

Socket Inheritance & Termination Two system calls used to start new application programs n fork Creates separate copy of currently executing process New copy inherits all open file descriptors & sockets n exec Desired application program loaded into the process Still retains access to inherited sockets (and FDs)

Both old and new processes share the existing sockets n OS keeps a count

Both old and new processes share the existing sockets n OS keeps a count associated with each socket Knows how many applications are using it n Programmer must ensure it is done in a meaningful way To close a socket: close(socket) n n Or, open sockets closed upon process termination close actually decrements count; destroy socket when it reaches zero

Specifying a Local Address Upon creation, sockets have no association to local or destination

Specifying a Local Address Upon creation, sockets have no association to local or destination addresses n TCP/IP No local protocol port number has been assigned No destination port or IP address has been specified n Client may not care what local address is Server process at well-know port will care Uses bind function to establish specific local addrs

Connecting Sockets to Destination Addresses Initially, sockets are unconnected n n Not associated with

Connecting Sockets to Destination Addresses Initially, sockets are unconnected n n Not associated with any remote destination Function connect binds a permanent destination Form: connect(socket, destaddr, addrlen) Semantics depend upon underlying protocol Reliable stream service: build TCP connection Connectionless service: stores dest address locally

Sending Data Through a Sockets are used to transmit data n Five possible functions

Sending Data Through a Sockets are used to transmit data n Five possible functions write n n n write(socket, buffer, length) buffer contains the address of the data to be sent Works with connected sockets only writev n n n writev(socket, iovector, vectorlen) iovector contains a sequence of pointers to blocks of bytes Works with connected sockets only

send n n send(socket, message, length, flags) message gives the address of the data

send n n send(socket, message, length, flags) message gives the address of the data flags controls the transmission Works with connected sockets only sendto n n sendto(socket, message, length, flags, destaddr, addrlen) destaddr specifies the socket address structure sendmsg n n n sendmsg(socket, messagestruct, flags) messagestruct is a structure with the required information Used when sendto makes the program inefficient or hard to read

Receiving Data Through a Socket Analogous five input functions n n n read(descriptor, buffer,

Receiving Data Through a Socket Analogous five input functions n n n read(descriptor, buffer, length) readv(descriptor, iovector, vectorlen) recv(socket, buffer, length, flags) recfrom(socket, buffer, length, flags, fromaddr, addrlen) recmsg(socket, messagestruct, flags)

Text gives examples of many socket functions and library routines n Can read over

Text gives examples of many socket functions and library routines n Can read over at your convenience

How a Server Accepts Connections Once a socket is established, the server waits for

How a Server Accepts Connections Once a socket is established, the server waits for a connection n Uses function accept to do so newsock = accept(socket, addrlen) When a request arrives: n n addr and addrlen filled in New socket created that has destination connected to client Original socket still remains open Call to accept returns

n Server can handle connections one of two ways Iteratively n Server handles request,

n Server can handle connections one of two ways Iteratively n Server handles request, closes new socket, calls accept Concurrently n n n Master creates slave to handle request at new socket Closes its copy of the new socket Calls accept Slave closes socket when it finishes, and then terminates Multiple processes will be using same local protocol port Ok because pair of endpoints defines a connection Master server has wildcard in foreign destination All other processes have a specific foreign dest

Socket Library Calls Socket API also offers set of library routines n Perform useful

Socket Library Calls Socket API also offers set of library routines n Perform useful functions related to networking System calls pass control to computer’s OS Library routines are like other program procedures Figure 21. 5

n Many socket library routines provide database services Determine names of machines & network

n Many socket library routines provide database services Determine names of machines & network services Determine protocol port numbers Find out other related information n Sections 21. 19 - 21. 25 examine groups of library routines

Example Client & Server Text gives example C program using the socket API to

Example Client & Server Text gives example C program using the socket API to access TCP/IP protocols n n Implements simple whois client & server Client is an application that a user invokes Two arguments: name of remote machine and user Creates socket, uses TCP, binds socket to whois port n Server only slightly more complex Listens on well-know whois port Returns requested info from UNIX password file

Summary API for TCP/IP depends on details of OS n n Because TCP/IP protocol

Summary API for TCP/IP depends on details of OS n n Because TCP/IP protocol SW resides inside OS Interface not specified by protocol standard Examined socket API n n n Originally designed for BSD UNIX Widely used by many vendors (like Microsoft) Uses UNIX open-read-write-close paradigm

n To use TCP, application program must: Create socket Bind addresses to it Accept

n To use TCP, application program must: Create socket Bind addresses to it Accept incoming connections Communicate using read or write primitives Close when finished n n Many library routines available, also Socket interface is popular and widely supported If not have socket facilities in OS, often provide socket library n Underlying OS will use different set of system calls