Professional Documents
Culture Documents
c
Mani
Radhakrishnan and Jon Solworth
sockets
Contact Info
Mani Radhakrishnan
Office
4224 SEL
email
mradhakr @ cs . uic . edu
Office Hours
Tuesday 1 - 4 PM
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
Introduction
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
Socket characteristics
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
UDP (datagram)
Use of sockets:
I
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
Socket APIs
I
sockets
TCP
UDP
Connection-based communication
Server performs the following actions
I
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
TCP client
send,recv: (repeated)
shutdown
close
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
TCP-based sockets
server
client
socket
socket
bind
connect
listen
accept
close
send/recv
send/recv
shutdown
shutdown
close
close
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
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 s o c k e t ( i n t domain , i n t t y p e , i n t p r o t o c o l ) ;
sockets
TCP
UDP
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
Where
I
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
sockaddr
For the internet family:
struct sockaddr
sa family t
in port t
4
struct in addr
}
in {
s i n f a m i l y ; // = AF INET
sin port ;
// i s a p o r t number
sin addr ;
// an I P a d d r e s s
struct sockaddr un {
uint8 t
sun length ;
short
sun family ;
char
sun path [ 1 0 0 ] ;
//
// = AF LOCAL
// n u l l t e r m i n a t e d pathname
// ( 1 0 0 i s p o s i x 1 . g minimum )
6 }
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
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
int
size );
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
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
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
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, the
socket 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 processes
are preforked and are passed the socketId.
Concurrent single server: use select to simultaneously wait on all
open socketIds, and waking up the process only when
new data arrives.
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
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
0: default
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
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
0: default
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
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 )
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
connect
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
Denoting Connections
from IP
from port
protocol
to IP
to port
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
Port usage
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
gethostname
#i n c l u d e <u n i s t d . h>
2
i n t g e t h o s t n a m e ( c h a r hostname ,
s i z e t nameLength )
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
hostent structure
struct hostent {
c h a r h name ;
c h a r h a l i a s e s ;
4
int
h addrtype ;
int
h length ;
6
c h a r h a d d r l i s t
}
2
//
//
//
//
; //
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
n u l l t e r m i n a t e d a r r a y of a l t e r n a t i v e hostnames
h o s t a d d r e s s t y p e AF INET o r AF INET6
4 o r 16 b y t e s
IPv4 or IPv6 l i s t of a d d r e s s e s
TRY AGAIN
NO RECOVERY
NO DATA
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
Gethostbyname
Auxiliary functions
#i n c l u d e <n e t d b . h>
2
s t r u c t h o s t e n t g e t h o s t b y n a m e ( c o n s t c h a r hostname )
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
// Host t o n e t w o r k b y t e o r d e r f o r s h o r t s ( 1 6 b i t )
u i n t 1 6 t htons ( u i n t 1 6 t v ) ;
4 // Host t o n e t w o r k b y t e
uint 32t htonl ( uint 32t
6
// Network t o h o s t b y t e
8 u i n t 1 6 t ntohs ( u i n t 1 6 t
o r d e r f o r long (32 b i t )
v );
o r d e r f o r long (16 b i t )
v );
10 // Network t o h o s t b y t e o r d e r f o r l o n g ( 3 2 b i t )
uint 32t ntohl ( uint 32t v );
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
IP 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 and
the address as the number.
Hence, we have 4 representations:
I
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
inet pton
#i n c l u d e <a r p a / i n e t . h>
2
4
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
inet ntop
#i n c l u d e <a r p a / i n e t . h>
2
4
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
sockets
TCP
UDP
Concurrent Server
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
sockaddr in serverAddr ;
2 s o c k a d d r &s e r v e r A d d r C a s t = ( s o c k a d d r &) s e r v e r A d d r ;
4 // g e t a t c p / i p s o c k e t
i n t s o c k F d = s o c k e t ( AF INET , SOCK STREAM , 0 ) ;
6
b z e r o (& s e r v e r A d d r , 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 = AF INET ;
// h o s t I P # i n d o t t e d d e c i m a l f o r m a t !
10 i n e t p t o n ( AF INET , s e r v e r N a m e , s e r v e r A d d r . s i n a d d r ) ;
serverAddr . s i n p o r t = htons ( 1 3 ) ;
12
c o n n e c t ( sockFd , 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
// . . r e a d and w r i t e o p e r a t i o n s on s o c k F d . .
shutdown ( sockFd , 2 ) ;
16 c l o s e ( s o c k F d ) ;
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
Connectionless communication
socket
shutdown
close
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
Connectionless communication
client
socket
bind
sendto/
recvfrom
shutdown
close
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
UDP variations
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
sendto
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
recvfrom
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
Example: UDPserver
i n t s o c k e t I d = s o c k e t ( AF INET , SOCK DGRAM, 0 ) ;
2
4
6
8
10
12
14
// a l l o w c o n n e c t i o n t o any a d d r on h o s t
// f o r h o s t s w i t h m u l t i p l e n e t w o r k c o n n e c t i o n s
// and a s t s e r v e r p o r t .
s e r v e r A d d r . s i n f a m i l y = AF INET ;
serverAddr . sin port
= htons ( s e r v e r P o r t ) ;
s e r v e r A d d r . s i n a d d r . s a d d r = INADDR ANY ;
// a s s o c i a t e p r o c e s s w i t h p o r 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 ( a d d r ) ) ;
16
18
20
22
// r e c e i v e from a c l i e n t
int size = sizeof ( clientAddr );
recvfrom ( socketId , buffer , bufferSize ,
0 , c l i e n t A d d r C a s t , &s i z e ) ;
24
// r e p l y t o t h e c l i e n t j u s t r e c e i v e d from
sendto ( socketId , buffer , bufferSize ,
0 , clientAddrCast , size );
26
close ( socketId );
c
Mani
Radhakrishnan and Jon Solworth
sockets
TCP
UDP
Example: UDPclient
i n t s o c k e t I d = s o c k e t ( AF INET , SOCK DGRAM, 0 ) ;
2
4
6
8
10
12
14
16
// s p e c i f y s e r v e r a d d r e s s , p o r t
s e r v e r A d d r . s i n f a m i l y = AF INET ;
serverAddr . sin port
= htons ( s e r v e r P o r t ) ;
s t r u c t h o s t e n t hp = g e t h o s t b y n a m e ( hostName ) ;
memcpy ( ( c h a r )& s e r v e r A d d r . s i n a d d r ,
( c h a r ) hp>h a d d r , hp>h l e n g t h ) ;
// no n e e d t o b i n d i f n o t p e e rtop e e r
int size = sizeof ( serverAddr );
sendto ( socketId , buffer , bufferSize , 0 ,
serverAddrCast , s i z e ) ;
18
20
22
close ( socketId );
c
Mani
Radhakrishnan and Jon Solworth