accept stylesThere are basically three styles of using accept: Iterating server: Only one socket is opened at a time.. Forking server: After an accept, a child process is forked off to h
Trang 1Socket Programming in C/C++c
September 24, 2004
Trang 3Sockets are a protocol independent method of creating a
connection between processes Sockets can be either
I connection based or connectionless: Is a connection
established before communication or does each packet
describe the destination?
I packet based or streams based: Are there message boundaries
or is it one stream?
I reliable or unreliable Can messages be lost, duplicated,reordered, or corrupted?
Trang 4Socket characteristics
Socket are characterized by their domain, type and transportprotocol Common domains are:
Common types are:
virtual circuit: received in order transmitted and reliablydatagram: arbitrary order, unreliable
Trang 5Socket characteristics (cont’d)
Each socket type has one or more protocols Ex:
I TCP/IP (virtual circuits)
Trang 6Socket APIs
(buy a phone)
can be queued for a server socket (call waiting allowance)
(answer phone)
(call)
Trang 7Connection-based communication
Server performs the following actions
requests that can be pending for this process
(repeated)
Trang 8TCP client
Client performs the following actions
Trang 10is the same as a file descriptor.
two way communication with maximum message size (This isnot available on most machines.)
withindomain
Trang 11address structure
Associates a socket id with an address to which other processescan connect In internet protocol the address is [ipNumber,portNumber]
Trang 12When using internet sockets, the second parameter of bind (of
Trang 14#i n c l u d e <s y s / t y p e s h>
2 #i n c l u d e <s y s / s o c k e t h>
4 i n t a c c e p t ( i n t s i d , s t r u c t s o c k a d d r ∗ a d d r P t r , i n t ∗ l e n P t r )
Returns the socketId and address of client connecting to socket
called, returns the actual value
Waits for an incoming request, and when received creates a socketfor it
Trang 15accept styles
There are basically three styles of using accept:
Iterating server: Only one socket is opened at a time When the
processing on that connection is completed, thesocket is closed, and next connection can beaccepted
Forking server: After an accept, a child process is forked off to
handle the connection Variation: the child processesare preforked and are passed the socketId
Concurrent single server: use selectto simultaneously wait on all
open socketIds, and waking up the process only whennew data arrives
Trang 16Pro and Con of Accept styles
I Iterating server is basically a low performance technique sinceonly one connection is open at a time
I Forking servers enable using multiple processors But theymake sharing state difficult, unless performed with threads.Threads, however present a very fragile programming
environment
I Concurrent single server: reduces context switches relative toforking processes and complexity relative to threads But doesnot benefit from multiprocessors
Trang 22I 0-1023: These ports can only be binded to by root
I 1024-5000: well known ports
I 5001-64K-1: ephemeral ports
Trang 23APIs for managing names and IP addresses
We next consider a number of auxiliary APIs:
presentation and strings
Trang 25Error is return throughh errorwhich can be:
Trang 27Network byte ordering
Network ordering in big endian (Sparc is big endian, Intel is littleendian)
Trang 28IP Number translation
IP address strings to 32 bit number
In what follows, ’p’ stands for presentation
Hence, these routines translate between the address as a string andthe address as the number
Hence, we have 4 representations:
I IP number in host order
I IP number in network order
I Presentation (eg dotted decimal)
I Fully qualified domain name
Only the last needs an outside lookup to convert to one of theother formats
Trang 29returns 1 if OK, 0 if presentation error, -1 error
Wherefamilyis either AF INETorAF INET6
Finally,addrPtr points to either the 32 bit result (AF INET) or
128 bit result (AF INET6)
Trang 30returns 1 if OK, 0 if presentation error, -1 error
Where family is eitherAF INETor AF INET6
Finally,addrPtr points to either the 32 bit (AF INET) or 128 bit(AF INET6)
Length is the size of destination
Trang 31Example: TCP/IP Server Code
Without error checking
Trang 32Concurrent Server
To build a concurrent server:
I a fork is performed after the accept
I The child process closes listenFd, and communicates usingconnectFd
I The parent process closses connectFd, and then loops back tothe accept to wait for another connection request
Trang 33Example: TCP/IP Client code
Trang 34Connectionless communication
Communication is symmetric (peer-to-peer)
Trang 36UDP variations
It is not necessary for both sockets tobind
I The receiver gets the address of the sender
It is possible for a UDP socket toconnect
I In this case,send/recv (or write/read) must be used instead
I Asynchronous errors can be returned (using ICMP)
Trang 38from an unspecified sender.
Sender address returned inaddrPtr, of size *addrLengthPtr.Returns number of bytes receive or -1 on error