Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
Transcript of Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
-
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
1/40
sockets
Socket Programming in C/C++
c Mani Radhakrishnan and Jon Solworth
September 24, 2004
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
2/40
sockets
Contact Info
Mani Radhakrishnan
Office
4224 SEL
emailmradhakr @ cs . uic . edu
Office HoursTuesday 1 - 4 PM
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
3/40
sockets TCPUDP
Introduction
Sockets are a protocol independent method of creating aconnection between processes. Sockets can be either
connection based or connectionless: Is a connection
established before communication or does each packetdescribe the destination?packet based or streams based : Are there message boundariesor is it one stream?
reliable or unreliable. Can messages be lost, duplicated,reordered, or corrupted?
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
4/40
sockets TCPUDP
Socket characteristics
Socket are characterized by their domain, type and transportprotocol. Common domains are:
AF UNIX: address format is UNIX pathnameAF INET: address format is host and port number
Common types are:virtual circuit: received in order transmitted and reliably
datagram: arbitrary order, unreliable
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
5/40
sockets TCPUDP
Socket characteristics (contd)
Each socket type has one or more protocols. Ex:TCP/IP (virtual circuits)UDP (datagram)
Use of sockets:Connectionbased sockets communicate client-server: theserver waits for a connection from the clientConnectionless sockets are peer-to-peer: each process issymmetric.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
TCP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
6/40
sockets TCPUDP
Socket APIs
socket : creates a socket of a given domain, type, protocol(buy a phone)bind : assigns a name to the socket (get a telephone number)listen : species the number of pending connections that
can be queued for a server socket. (call waiting allowance)accept : server accepts a connection request from a client(answer phone)connect : client requests a connection request to a server
(call)send, sendto : write to connection (speak)recv, recvfrom : read from connection (listen)shutdown : end the call
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
TCP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
7/40
sockets TCPUDP
Connection-based communication
Server performs the following actionssocket : create the socketbind : give the address of the socket on the server
listen : species the maximum number of connectionrequests that can be pending for this processaccept : establish the connection with a specic clientsend,recv : stream-based equivalents of read and write(repeated)shutdown : end reading or writingclose : release kernel data structures
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
TCP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
8/40
sockets TCPUDP
TCP client
Client performs the following actionssocket : create the socket
connect : connect to a serversend,recv : (repeated)shutdown
close
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
TCP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
9/40
sockets TCPUDP
TCP-based sockets
bind
listen
accept close
send/recv
shutdown
close
socket
connect
send/recv
shutdown
close
server client
socket
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
TCP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
10/40
sockets TCPUDP
socket API# 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 sock e t ( i n t domain , i n t type , i n t p r o t o c o l ) ;
Returns a le descriptor (called a socket ID) if successful, -1otherwise. Note that the socket returns a socket descriptor whichis the same as a le descriptor.The domain is AF INET.The type argument can be:
SOCK STREAM: Establishes a virtual circuit for streamSOCK DGRAM: Establishes a datagram for communicationSOCK SEQPACKET: Establishes a reliable, connection based,two way communication with maximum message size. (This isnot available on most machines.)
protocol is usually zero, so that type denes the connection
within domain .c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
k TCP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
11/40
sockets TCPUDP
bind
# 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 bind ( i n t s i d , s t r u c t s o c k a d d r addrPtr , i n t l e n )
Where
sid : is the socket idaddrPtr : is a pointer to the address family dependentaddress structurelen : is the size of *addrPtr
Associates a socket id with an address to which other processescan connect. In internet protocol the address is [ipNumber,portNumber]
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
k t TCP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
12/40
sockets UDP
sockaddr
For the internet family:s t r u c t s o c k a d d r i n {
2 s a f a m i l y t s i n f a m i l y ; / / = AF INET i n p o r t t s i n p o r t ; // i s a p o r t n umber
4 s t r u c t i n a d d r s i n a d d r ; // an I P a d dr e ss }
For unix sockets (only works between processes on the samemachine)s t r u c t s o c ka d d r u n {
2 u i n t 8 t s u n l e n g t h ; // s h o r t s u n f a m i l y ; // = AF LOCAL
4 char s u n p a t h [ 1 0 0 ] ; // n u l l t e r m i n a t e d p a th na me // ( 10 0 i s p o s i x 1 . g minimum )
6 }
When using internet sockets, the second parameter of bind (of type sockaddr in * ) must be cast to (sockaddr *) .
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
13/40
sockets UDP
listen
# 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 l i s t e n ( i n t s i d , i n t s i z e ) ;
Where size it the number of pending connection requests allowed(typically limited by Unix kernels to 5).Returns the 0 on success, or -1 if failure.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
14/40
sockets UDP
accept
# 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 accep t ( i n t s i d , s t r u c t s o c k a d d r addrPtr , i n t l e n P t r )
Returns the socketId and address of client connecting to socket.if lenPtr or addrPtr equal zero, no address structure is returned.lenPtr is the maximum size of address structure that can becalled, returns the actual value.Waits for an incoming request, and when received creates a socketfor it.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
15/40
sockets UDP
accept 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 be
accepted.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 select to simultaneously wait on allopen socketIds, and waking up the process only whennew data arrives.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
16/40
sockets UDP
Pro and Con of Accept styles
Iterating server is basically a low performance technique sinceonly one connection is open at a time.Forking servers enable using multiple processors. But they
make sharing state difficult, unless performed with threads.Threads, however present a very fragile programmingenvironment.Concurrent single server: reduces context switches relative to
forking processes and complexity relative to threads. But doesnot benet from multiprocessors.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
17/40
UDP
send
# 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 sen d ( i n t s i d , c o ns t c h ar b u f f e r P t r ,i n t l en , i n t f l a g )
Send a message. Returns the number of bytes sent or -1 if failure.(Must be a bound socket).ag is either
0: default
MSG OOB: Out-of-band high priority communication
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
18/40
UDP
recv
# 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 r ec v ( i n t s i d , cha r b u f f e r P t r ,i n t l en , i n t f l a g s )
Receive up to len bytes in bufferPtr . Returns the number of bytes received or -1 on failure.ags can be either
0: default
MSG OOB: out-of-bound messageMSG PEEK: look at message without removing
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
19/40
UDP
shutdown
# 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 shutdown( i n t s i d , i n t how)
Disables sending (how=1 or how=2) or receiving (how=0 orhow=2). Returns -1 on failure.acts as a partial close.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
20/40
UDP
connect
this is the rst of the client calls
# 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 conne ct ( i n t s i d , s t r u c t s o c k a d d r addrPtr , i n t l e n )
Species the destination to form a connection with (addrPtr), andreturns a 0 if successful, -1 otherwise.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
21/40
U
Denoting Connections
Note that a connection is denoted by a 5-tuple:from IPfrom portprotocolto IPto port
So that multiple connections can share the same IP and port.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
22/40
Port usage
Note that the initiator of communications needs a xed port totarget communications.This means that some ports must be reserved for these well
known ports.Port usage:0-1023: These ports can only be binded to by root1024-5000: well known ports
5001-64K-1: ephemeral ports
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
23/40
APIs for managing names and IP addresses
We next consider a number of auxiliary APIs:The hostent structure: describes IP, hostname pairs
gethostbyname : hostent of a specied machinehtons, htonl, ntohs, ntohl : byte orderinginet pton, inet ntop : conversion of IP numbers betweenpresentation and strings
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
24/40
gethostname
# i n c l u d e < u n i s t d . h >2
i n t gethostname ( cha r h os tn am e , s i z e t n am eL en gt h )
Returns the hostname of the machine on which this commandexecutes (What host am i?). Returns -1 on failure, 0 on success.MAXHOSTNAMELENis dened in .
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
25/40
hostent structure
s t r u c t h o s t e n t {2 char h name ; / / o f f i c i a l ( c a n o n i c a l ) name o f t h e h o s t
char h a l i a s e s ; // n u l l t er mi na te d a r ra y o f a l t e r n a t i v e hostnames 4 i n t h a d d r t y p e ; // h o s t a d d r e s s t y p e AF INET o r AF INET6
i n t h l e n g t h ; // 4 o r 16 b yt es 6 char h a d d r l i s t ; // I Pv4 o r I Pv6 l i s t o f a d d r es s e s
}
Error is return through h error which can be:HOST NOT FOUND
TRY AGAIN
NO RECOVERYNO DATA
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
26/40
Gethostbyname
Auxiliary functions# i n c l u d e < ne tdb . h >
2s t r u c t h o s t e n t gethostbyname ( c o n st c h ar hostname)
Translates a DNS name into a hostent.Example:s t r u c t h o s t e n t h o s t E n t i t y =
2 g e t ho s tb y n am e ( b e r t . c s . u i c . e du ) ;memcpy( soc ke tAd dr > s i n a d d r ,
4 h o s t E n t i t y > h a d d r l i s t [ 0 ] ,
h o s t E n t i t y > h l e n g t h ) ;
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
27/40
Network byte ordering
Network ordering in big endian. (Sparc is big endian, Intel is littleendian).// Host t o n et wo rk b yt e o r de r f o r s h o r t s ( 16 b i t )
2 u i n t 1 6 t h to ns ( u i n t 1 6 t v ) ;
4 // Host t o n et wo rk b yt e o r de r f o r l on g ( 32 b i t )u i n t 3 2 t h t on l ( u i n t 3 2 t v ) ;
6// Network t o h os t b yt e o r de r f o r l on g ( 16 b i t )
8 u i n t 1 6 t n to hs ( u i n t 1 6 t v ) ;
10 // Network t o h os t b yt e o r de r f o r l on g ( 32 b i t )u i n t 3 2 t n t oh l ( u i n t 3 2 t v ) ;
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
28/40
IP Number translation
IP address strings to 32 bit numberIn 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:
IP number in host orderIP number in network orderPresentation (eg. dotted decimal)
Fully qualied domain nameOnly the last needs an outside lookup to convert to one of theother formats.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
29/40
inet pton
# i n c l u d e < a r p a / i n e t . h >2
i n t i n e t p t o n ( i n t f a m i l y , c o ns t c h ar s t r P t r ,4 v o i d a d d r P t r ) ;
returns 1 if OK, 0 if presentation error, -1 errorWhere family is either AF INET or AF INET6.The strPtr is the IP address as a dotted string.Finally, addrPtr points to either the 32 bit result (AF INET) or
128 bit result (AF INET6
).
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
30/40
inet ntop
# i n c l u d e < a r p a / i n e t . h >2
i n t i n e t n t o p ( i n t f a m i l y , c o ns t c h ar addrPtr ,4 cha r s tr Pt r , s i z e t l en ) ;
returns 1 if OK, 0 if presentation error, -1 errorWhere family is eitherAF INET or AF INET6.The strPtr is the return IP address as a dotted string.Finally, addrPtr points to either the 32 bit (AF INET) or 128 bit(AF INET6).Length is the size of destination.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
31/40
Example: TCP/IP Server CodeWithout error checking.
s o c k a d d r i n s e r v er A d d r ;2 s o c ka d d r & s e r v e r A d d r C a st = ( s o c ka d d r &) s e r v e r A d d r ;
4 // g et a t cp / i p s oc ke t i n t l i s t en Fd = soc ke t (AF INET , SOCK STREAM, 0) ;
6bze ro (&serve rAddr , s i z e o f ( s e r v e r A d d r ) ) ;
8 s e r v e r A d d r . s i n f a m i l y = A F INET ;// any i n t e rn e t i n t e r f a c e on t h i s s e rv e r .
10 se rv e rA ddr . s i n ad dr . s add r = h to n l ( INADDR ANY) ;s e r v e r A d d r . s i n p o r t = h t o ns ( 1 3 ) ;12
b i n d ( l i s t e n F d , &s e r v e r A d d r C a s t , s i z e o f ( s e r v e r A d d r ) ) ;14
l i s t e n ( l i s t en Fd , 5 ) ;16
f o r ( ; ; ) {18 i n t connec tFd =
a c c e p t ( l i s t e n F d , ( s o c k a d d r ) NULL, NULL ) ;20 // . . r ea d and w r i t e o p e r a t io n s on c on ne ct Fd . .
shutdown( connec tFd , 2 ) ;22 c l os e ( connec tFd ) ;
}
Note that the above is an iterative server, which means that it
serves one connection at a time.c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
32/40
Concurrent Server
To build a concurrent server:a fork is performed after the accept.
The child process closes listenFd, and communicates usingconnectFd.The parent process closses connectFd, and then loops back tothe accept to wait for another connection request.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
33/40
Example: TCP/IP Client code
s o c k a d d r i n s e r v er A d d r ;2 s o c ka d d r & s e r v e r A d d r C a st = ( s o c ka d d r &) s e r v e r A d d r ;
4 // g et a t cp / i p s oc ke t i n t sock Fd = so c ke t (AF INET , SOCK STREAM, 0 ) ;
6
bze ro (&serve rAddr , s i z e o f ( s e r v e r A d d r ) ) ;8 s e r v e r A d d r . s i n f a m i l y = A F INET ;// h o st I P # i n d o tt e d d e ci m a l f or m at !
10 i n e t p t o n ( AF INET , s e rv er N am e , s e r v e r A d d r . s i n a d d r ) ;s e r v e r A d d r . s i n p o r t = h t o ns ( 1 3 ) ;
12connec t ( sockFd , s e rve r AddrCa s t , s i z e o f ( s e r v e r A d d r ) ) ;
14 // . . r ea d and w r i t e o p e r a t io n s on so ck Fd . .shutdown( sockFd , 2 ) ;
16 c l os e ( sockFd ) ;
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
34/40
Connectionless communication
Communication is symmetric (peer-to-peer)socket
bind : bind is optional for initiatorsendto, recvfrom (repeated)shutdown
close
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
35/40
Connectionless communication
socket
bind
sendto/recvfrom
shutdown
close
client
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
36/40
UDP variations
It is not necessary for both sockets to bind
The receiver gets the address of the sender
It is possible for a UDP socket toconnectIn this case, send/recv (or write/read) must be used insteadof sendto/recvfrom .Asynchronous errors can be returned (using ICMP)
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
37/40
sendto
for connectionless protocols# 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 send to ( i n t s i d , c o ns t v o i d b u f f e r P t r ,s i z e t b u ff e rL e ng th , i n t f l a g ,
6 s t r u c t s o c k a d d r a d dr P tr , s o c k l e n t a d dr L en g th )
Send a buffer, bufferPtr , of length bufferLength to addressspecied by addrPtr of size addrLength . Returns number of bytes sent or -1 on error.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
38/40
recvfrom
for connectionless protocols# 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 r ecvf r om ( i n t s i d , v o i d b u f f e r P t r , i n t b u f f e r L e n g t h ,i n t f l a g , s o ck a dd r addrPtr , i n t addrLeng thP t r )
Receive a buffer inbufferPtr of maximum length bufferLengthfrom an unspecied sender.Sender address returned in addrPtr , of size *addrLengthPtr .
Returns number of bytes receive or -1 on error.
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
39/40
Example: UDPserver
i n t so ck et Id = so ck et (AF INET , SOCK DGRAM, 0) ;
2 s o c k a d d r i n s e rv e rA d dr , c l i e n t A d d r ;4 s o ck a dd r &s e r v er A d d rC a s t = ( s oc k ad d r &) s e r v e rA d d r ;
s o c k ad d r &c l i e n t A d d r C a s t = ( s o c ka d d r &) c l i e n t A d d r ;6
// a l l ow c o nn e ct i on t o any a dd r o n h os t 8 // f o r h o st s w it h m u l t i p l e n et wo rk c o nn e c ti o ns
// and a s t s e r v e r p o rt .10 s e r v er A d d r . s i n f a m i l y = AF INET ;
s e r v er A d d r . s i n p o r t = h t o ns ( s e r v e r P o r t ) ;12 s e r v e r A d d r . s i n a d d r . s a d d r = INADDR ANY;
14 // a s s o ci a t e p r oc e ss w it h p or t b i n d ( s o c k e t I d , &s e r v e r A d d r C a s t , s i z e o f ( addr ) ) ;
16// r e c e iv e from a c l i e n t
18 i n t s i z e = s i z e o f ( c l i e n t A d d r ) ;r e c vf r o m ( s o c ke t I d , b u f f e r , b u f f e r S i z e ,
20 0 , c l i e n t A d d r C a s t , &s i z e ) ;
22 // r e p l y to th e c l i e n t j u s t r e ce i ve d f roms e nd t o ( s o c ke t I d , b u f f e r , b u f f e r S i z e ,
24 0 , c l i e n t A d d r C a s t , s i z e ) ;
26 c l o se ( s o ck e t I d ) ;
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
sockets TCPUDP
http://goforward/http://find/http://goback/ -
8/8/2019 Http Subjects.ee.Unsw.edu.Au Tele3118 Wk6 Sockets
40/40
Example: UDPclient
i n t so ck et Id = so ck et (AF INET , SOCK DGRAM, 0) ;2
s o c k a d d r i n s e rv e rA d dr , c l i e n t A d d r ;4 s o ck a dd r &s e r v er A d d rC a s t = ( s oc k ad d r &) s e r v e rA d d r ;
s o c k ad d r &c l i e n t A d d r C a s t = ( s o c ka d d r &) c l i e n t A d d r ;6
// s p e c i f y s e r v e r a dd re ss , p or t 8 s e rv e rA d dr . s i n f a m i l y = AF INET ;
s e r v er A d d r . s i n p o r t = h t o ns ( s e r v e r P o r t ) ;10 s t r u c t h o s t e n t hp = gethostbyname ( hostName ) ;
memcpy(( cha r )& s e r v e r A d d r . s i n a d d r ,12 ( cha r )hp> h a d d r , hp> h l e n g t h ) ;
14 // no n ee d t o b in d i f n ot p ee r to p e e r i n t s i z e = s i z e o f ( s e r v e r A d d r ) ;
16 s en dt o ( s o ck e tI d , b u f fe r , b u f f e r S i ze , 0 ,s e r v e rA d d r C as t , s i z e ) ;
18
r e c vf r o m ( s o c ke t I d , b u f f e r , b u f f e r S i z e , 0 ,20 se r v e r A d d r C a s t , &s i z e ) ;
22 c l o se ( s o ck e t I d ) ;
c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++
http://goforward/http://find/http://goback/