Chapter 21

standguideΔίκτυα και Επικοινωνίες

26 Οκτ 2013 (πριν από 3 χρόνια και 10 μήνες)

131 εμφανίσεις

The Socket Interface

Chapter 21

Application Program Interface (API)


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


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


Operations listed are NOT part of the TCP/IP standards

UNIX I/O Paradigm

Unix is a
process

oriented OS


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


Call
open

to get file descriptor for 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 protocols to UNIX


Interactions more complex

Passive server code as well as active client code

Specify datagram address when send vs when open


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


Mechanism that provides an endpoint for
communication


Create when needed; get integer to reference


Application can choose address binding method

TCP connection


Both endpoints must be specified

Use for UDP


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, protocol)


pf

specifies protocol family


type

specifies communication type

Reliable stream; connectionless datagram; raw


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


fork

Creates separate copy of currently executing process

New copy inherits all open file descriptors & sockets


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


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

To close a socket:



close(socket)


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 addresses


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

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


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 Socket

Sockets are used to transmit data


Five possible functions

write



write(socket, buffer, length)


buffer

contains the address of the data to be sent


Works with connected sockets only

writev


writev(socket, iovector, vectorlen)


iovector

contains a sequence of pointers to blocks of bytes


Works with connected sockets only

send


send(socket, message, length, flags)


message

gives the address of the data


flags

controls the transmission


Works with connected sockets only

sendto


sendto(socket, message, length, flags, destaddr,
addrlen)


destaddr

specifies the socket address structure

sendmsg


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


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


Can read over at your convenience

How a Server Accepts Connections

Once a socket is established, the server
waits for a connection


Uses function
accept

to do so

newsock = accept(socket, addr, addrlen)

When a request arrives:


addr

and
addrlen

filled in


New socket created that has destination connected to client


Original socket still remains open


Call to
accept

returns


Server can handle connections one of two ways

Iteratively


Server handles request, closes new socket, calls
accept

Concurrently


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


Perform useful functions related to networking

System calls pass control to computer’s OS

Library routines are like other program procedures

Figure 21.5


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

Example Client & Server

Text gives example C program using the
socket API to access TCP/IP protocols


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


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


Because TCP/IP protocol SW resides inside OS


Interface not specified by protocol standard

Examined socket API


Originally designed for BSD UNIX


Widely used by many vendors (like Microsoft)


Uses UNIX open
-
read
-
write
-
close paradigm


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


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