Distributed Programming With Sockets: Wondimagegn D
Distributed Programming With Sockets: Wondimagegn D
Wondimagegn D.
3 TCP
4 I/O Multiplexing
5 Server Structures
Introduction
Circuit switching
One electrical circuit assigned per communication
Example: the (analog) phone network
Guaranteed constant quality of service
Waste of resources (periods of silence), fault tolerance
Packet switching
Messages are split into packets, which are transmitted independently
Packets can take different routes
Network infrastructures are shared among users
Example: the Internet, and most computer networks
Good resource usage, fault tolerance
Variable QOS, packets may be delivered in the wrong order
Internet Protocol
IP Address Conversion
IP Addresses
32-bit integers: 2183468070 (good for computers!)
Dotted strings: 130.37.20.38 (good for humans!)
DNS name: www.aait.edu.et (even better for humans!)
You can convert between integer and dotted string:
IP Address Conversion
IP Addresses
32-bit integers: 2183468070 (good for computers!)
Dotted strings: 130.37.20.38 (good for humans!)
DNS name: www.aait.edu.et (even better for humans!)
You can convert between integer and dotted string:
#i n c l u d e <a r p a / i n e t . h>
i n a d d r t i n e t a d d r ( c o n s t c h a r ∗ d o t t e d ) ; /∗ D o t t e d t o Network ∗/
c h a r ∗ i n e t n t o a ( s t r u c t i n a d d r n e t w o r k ) ; /∗ Network t o D o t t e d ∗/
#i n c l u d e <n e t i n e t / i n . h>
struct sockaddr in {
s a f a m i l y t s i n f a m i l y ; /∗ s e t t o AF INET ∗/
i n p o r t t s i n p o r t ; /∗ P o r t number ∗/
s t r u c t i n a d d r s i n a d d r ; /∗ C o n t a i n s t h e IP a d d r e s s ∗/
};
struct in addr {
i n a d d r t s a d d r ; /∗ IP a d d r e s s i n n e t w o r k o r d e r i n g ∗/
};
Domain Names
#i n c l u d e <n e t d b . h>
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 ∗name ) ;
struct hostent {
c h a r ∗h name ; /∗ o f f i c i a l name o f h o s t ∗/
c h a r ∗∗ h a l i a s e s ; /∗ a l i a s l i s t ∗/
int h addrtype ; /∗ h o s t a d d r e s s t y p e ∗/
i n t h l e n g t h ; /∗ l e n g t h o f a d d r e s s ∗/
c h a r ∗∗ h a d d r l i s t ; /∗ l i s t o f a d d r e s s e s ∗/
};
gethostbyname()
Example
#i n c l u d e <n e t d b . h>
i n t p r i n t r e s o l v ( c o n s t c h a r ∗name ) {
struct hostent ∗resolv ;
s t r u c t i n a d d r ∗addr ;
r e s o l v = g e t h o s t b y n a m e ( name ) ;
i f ( r e s o l v==NULL) {
p r i n t f ( ” A d d r e s s n o t f o u n d f o r %s \n” , name ) ;
r e t u r n −1;
}
else {
a d d r = ( s t r u c t i n a d d r ∗) r e s o l v −>h a d d r l i s t [ 0 ] ;
p r i n t f ( ”The IP a d d r e s s o f %s i s %s \n” , name , i n e t n t o a (∗ a d d r ) ) ;
return 0;
}
}
TCP Sockets
Creating a Socket
listen()
accept():
#i n c l u d e <s y s / t y p e s . h>
#i n c l u d e <s y s / s o c k e t . h>
i n t a c c e p t ( i n t s o c k f d , s t r u c t s o c k a d d r ∗addr , s o c k l e n t ∗ a d d r l e n ) ;
Example
i n t s o c k , newsock , r e s ;
sockaddr in client addr ;
socklen t addrlen ;
( t h e s o c k e t s o c k i s c r e a t e d and bound )
r e s = l i s t e n ( sock , 5 ) ;
i f ( r e s < 0) { . . . }
addrlen = sizeof ( struct sockaddr in );
newsock = a c c e p t ( s o c k , ( s t r u c t s o c k a d d r ∗) &c l i e n t a d d r , &a d d r l e n ) ;
i f ( newsock < 0 ) { . . . }
else
{
p r i n t f ( ” C o n n e c t i o n from %s !\ n” , i n e t n t o a ( c l i e n t a d d r . s i n a d d r ) ) ;
}
write works the same for sending data to a TCP socket or writing to
a file
#i n c l u d e <u n i s t d . h>
s s i z e t w r i t e ( i n t s o c k f d , c o n s t v o i d ∗buf , s i z e t count ) ;
read() blocks the process until receiving data from the socket
#i n c l u d e <u n i s t d . h>
s s i z e t r e a d ( i n t s o c k f d , v o i d ∗buf , s i z e t count ) ;
#i n c l u d e <u n i s t d . h>
int close ( int sockfd );
Asymmetric Disconnection
Sometimes you may want to tell the other party that you are finished,
but let it finish before closing the connection
#i n c l u d e <s y s / s o c k e t . h>
i n t shutdown ( i n t s o c k f d , i n t how ) ;
I/O Multiplexing
Server Structures
Iterative Servers
i n t f d , newfd ;
while (1) {
newfd = a c c e p t ( fd , . . . ) ;
t r e a t r e q u e s t ( newfd ) ;
c l o s e ( newfd ) ;
}
Simple
Potentially low resource utilization
- If treat request() does not utilize all the CPU, resources are wasted
Potentially long waiting queue of incoming connections waiting to be
accept()ed
Increased request treatment latency
If the queue increases, the server may start rejecting incoming
connections
void s i g c h l d ( int ) {
w h i l e ( w a i t p i d ( 0 , NULL ,WNOHANG)>0) {}
s i g n a l ( SIGCHLD , s i g c h l d ) ;
}
i n t main ( ) {
i n t f d , newfd , p i d ;
s i g n a l ( SIGCHLD , s i g c h l d ) ;
while (1) {
newfd = a c c e p t ( fd , . . . ) ;
i f ( newfd <0) c o n t i n u e ;
pid = fork ( ) ;
i f ( p i d ==0) { t r e a t r e q u e s t ( newfd ) ; e x i t ( 0 ) ; }
else
{ c l o s e ( newfd ) ; }
}
}
Preforked
#d e f i n e NB PROC 10
v o i d r e c v r e q u e s t s ( i n t f d ) { /∗ An i t e r a t i v e s e r v e r ∗/
int f ;
while (1) {
f=a c c e p t ( fd , . . . ) ;
treat request ( f );
close ( f );
}
}
i n t main ( ) {
i n t fd ;
f o r ( i n t i =0; i<NB PROC ; i ++) { /∗ C r e a t e NB PROC c h i l d r e n ∗/
i f ( f o r k ()==0) r e c v r e q u e s t s ( f d ) ;
}
w h i l e ( 1 ) p a u s e ( ) ; /∗ The p a r e n t p r o c e s s d o e s n o t h i n g ∗/
}
Preforked
Select Loop