Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.

Similar presentations


Presentation on theme: "The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between."— Presentation transcript:

1 The Socket Interface Chapter 21

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

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

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

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

6 Creating a Socket Use socket function to create a socket result = socket(pf, type, protocol) result = socket(pf, type, protocol) pf specifies protocol family pf specifies protocol family type specifies communication type type specifies communication type Reliable stream; connectionless datagram; raw protocol specifies a specific protocol in the family 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

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

8

9

10

11

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

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

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

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

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

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

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

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

20 Server can handle connections one of two ways Server can handle connections one of two waysIteratively Server handles request, closes new socket, calls accept Server handles request, closes new socket, calls acceptConcurrently Master creates slave to handle request at new socket 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 Slave closes socket when it finishes, and then terminates Multiple processes will be using same local protocol port 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

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

22 Many socket library routines provide database services Many socket library routines provide database services Determine names of machines & network services Determine protocol port numbers Find out other related information Sections 21.19 - 21.25 examine groups of library routines Sections 21.19 - 21.25 examine groups of library routines

23 Example Client & Server Text gives example C program using the socket API to access TCP/IP protocols Implements simple whois client & server Implements simple whois client & server Client is an application that a user invokes 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 Server only slightly more complex Server only slightly more complex Listens on well-know whois port Returns requested info from UNIX password file

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

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


Download ppt "The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between."

Similar presentations


Ads by Google