Documente Academic
Documente Profesional
Documente Cultură
2nd Edition
• Contents
– Introduction
– A Simple Daytime Client
– Error Handling: Wrapper Functions
– A Simple Daytime Server
– OSI Model
– Unix Standards
Client
...
Client Server
...
Client
Figure 1.2 Server handling multiple clients at the same time.
UNIX Network Programming 3
1.1 Introduction (continued)
• Example : Client and Server on the same Ethernet
communication using TCP
User Application protocol
Web Web Application layer
process Client server
TCP protocol
TCP TCP transport layer
Protocol stack
within kernel
IP IP protocol IP network layer
Ethernet
Figure 1.3 Client and server on the same Ethernet communicating using TCP
Host Host
with with
TCP/IP TCP/IP
LAN LAN
router router
WAN
router router router router
Figure 1.4 Client and server on different LANs connected through a WAN
int
main (int argc, char **argv)
{
int sockfd, n;
char recvline[MAXLINE+1];
struct sockaddr_in servaddr;
• bzero(&servaddr, sizeof(servaddr));
• servaddr.sin6_family = AF_INET6;
• servaddr.sin6_port = htons(13); /* daytime server */
• if (inet_pton(AF_INET6, argv[1], &servaddr.sin6_addr) <= 0)
• err_quit("inet_pton error for %s", argv[1]);
• exit(0);
• }
int
main(int argc, char **argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[MAXLINE];
time_t ticks;
Listen(listenfd, LISTENQ);
3 for( ; ; ) {
connfd = Accept(listenfd, (SA *) NULL, NULL) ;
ticks = time(NULL);
snprintf(buff, sizeof(buff), “%.24s\r\n”, ctime(&ticks) );
4 Write(connfd, buff, strlen(buff) );
Close(connfd);
}
}
5 Session Sockets
4 Transport TCP | | UDP XTI
3 Network IPv4, IPv6 kernel
communication
2 Datalink Device driver details
1 Physical and Hardware
OSI Model Internet protocol
suite
Figure 1.14 Layers on OSI model and Internet protocol suite
– Why do both sockets and XTI provide the interface from the upper three layers of the
OSI model into the transport layer?
• First, the upper three layers handle all the details of the application and The lower four
layers handle all the communication details.
• Second, the upper three layers is called a user process while the lower four layers are
provided as part of the operating system kernel.
tc p - m - tra c e - tra c e -
p in g a p p l. a p p l. a p p l. a p p l. p in g
dum p ro u te d ro u te ro u te
A P I
TC P U D P
IC M P
3 2 - b it 1 2 8 - b it IC M P v
IG M P IP v 4 IP v 6
a d d re s s e s a d d re s s e s 6
AR P,
R AR P
B PF, d a ta -
D LPI lin k
F ig u re 2 . 1 O v e rv ie w o f T C P / IP p ro to c o ls
c o n n e c t re tu rn s
a c c e p t re tu rn s
r e a d ( b lo c k s )
F ig u re 2 . 2 T C P th re e - w a y h a n d s h a k e
• SYN segment
• ACK
0 15 16 32
1 6 - b it s o u rc e p o rt n u m b e r 1 6 - b it d e s tin a tio n p o rt n u m b e r
3 2 - b it s e q u e n c e n u m b e r
3 2 - b it a c k n o w le d g m e n t n u m b e r 2 0 b y te s
U A P R S F
4 - b it h e a d e r re s e rv e d
R C S S Y I 1 6 - b it w in d o w s iz e
le n g th ( 6 b it) G K H T N N
1 6 - b it T C P c h e c k s u m 1 6 - b it u rg e n t p o in te r
o p tio n ( if a n y )
d a ta ( if a n y )
TC P H eader
a p p lic a t io n
Appl
u s e r d a ta
header
TC P
TC P
a p p lic a tio n d a ta
header
TC P segm ent IP
IP TC P
a p p lic a tio n d a ta
header header
IP d a ta g ra m E th e rn e t
d riv e r
E th e rn e t IP TC P E th e rn e t
a p p lic a tio n d a ta
header header header tra ile r
E th e rn e t
14 20 20 4
E th e rn e t f ra m e
4 6 to 1 5 0 0 b y te s
E n c a p s u la t io n o f d a ta a s it g o s e d o w n th e p ro to c o l s ta c k s
• TCP Options
– MSS option
• With this option the TCP sending the SYN announces its maximum segment
size, the maximum amount of data that it is willing to accept in each TCP
segment, on this connection.
– Timestamp option
close
( a c t iv e c lo s e ) FIN M
( p a s s iv e c lo s e )
r e a d re tu rn s 0
1
a ck M +
close
c o n n e c t re tu rn s
FIN N
ack N +
1
F ig u re 2 . 3 P a c k e t s e x c h a n g e d w h e n a T C P c o n n e c t io n is c lo s e d .
• half-close : Between steps 2 and 3 it is possible for data to flow from the end doing
the passive close to the end doing active close.
C LO SED
2.5 TCP Co a p p l: p a s s iv e o p e n
nnection Estab
s e n d : < n o th in g >
lishment and T L IS T E N
p a s s iv e o p e n
ermination (con
re c v : S Y N
t) S Y N _R C V D
send: SYN , AC K
S YN _S E N T
a p p l: c lo s e
o r tim e o u t
s im u lta n e o u s o p e n a c tiv e o p e n
re c v : F IN
E S T A B L IS H E D C L O S E _ W A IT
send: AC K
d a ta tra n s f e r s ta te
re c v : c lo s e
send: F IN
- State s im u lta n e o u s c lo s e
re c v : A C K
transition
re c v : F IN
F IN _ W A IT _ 1 C L O S IN G LA S T_A C K
send: AC K s e n d : < n o th in g >
diagram re c v : A C K re c v : A C K
p a s s iv e c lo s e
re c v : F IN
F IN _ W A IT _ 2 T IM E _ W A IT
send: AC K
2 M S L tim e o u t
a c tiv e c lo s e
F ig u re 2 . 4 T C P s t a t e t r a n s it io n d ia g ra m
UNIX Network Programming 22
2.5 TCP Connection Establishment and Termination (cont)
c lie n t s e rv e r
• Watching the Packets socket
c o n n e c t ( b lo c k s )
socket,bind,listen
LISTEN(passive open)
a c c p e t ( b lo c k s )
( a c tio n o p e n )
S YN _S E N T
ESTABLISHED
c o n n e c t re tu rn s
r e a d re tu rn s
<server processes request>
write
r e a d ( b lo c k s )
r e a d re tu rn s
close
( a c tiv e c lo s e )
F IN _ W A IT _ 1
C L O S E _ W A IT
( p a s s iv e c lo s e )
r e a d re tu rn s 0
close
F IN _ W A IT _ 2 LAST_AC K
T IM E _ W A IT
C LO SED
F ig u re 2 . 5 P a c k e t e x c h a n g e f o r T C P c o n n e c tio n
c o n n e c tio n re q u e s t to
s e rv e r c lie n t
2 0 6 . 6 2 . 2 2 6 . 3 5 , p o rt 2 1
lis te n in g s o c k e t (* .2 1 , * .* ) {1 9 8 .6 9 .1 0 .2 .1 5 0 0 ,
2 0 6 .6 2 .2 2 6 .3 5 .2 1 }
F ig u re 2 . 8 C o n n e c t io n re q u e s t f ro m c lie n t to s e rv e r
2 0 6 .6 2 .2 2 6 .3 5
2 0 6 .6 2 .2 2 6 .6 6 1 9 8 .6 9 .1 0 .2
s e rv e r c lie n t
lis te n in g s o c k e t (* .2 1 , * .* ) {1 9 8 .6 9 .1 0 .2 .1 5 0 0 ,
on 2 0 6 .6 2 .2 2 6 .3 5 .2 1 }
e cti
fork nn
co
s e rv e r
( c h ild )
c o n n e c te d
socket {2 0 6 .6 2 .2 2 6 .3 5 .2 1 ,
1 9 8 .6 9 .1 0 .2 .1 5 0 0 }
s e rv e r c lie n t 1
lis te n in g s o c k e t (* .2 1 , * .* ) {1 9 8 .6 9 .1 0 .2 .1 5 0 0 ,
n
ectio 2 0 6 .6 2 .2 2 6 .3 5 .2 1 }
n n
fork co
s e rv e r
c lie n t 2
( c h ild 1 )
c o n n e c te d
socket {2 0 6 .6 2 .2 2 6 .3 5 .2 1 , {1 9 8 .6 9 .1 0 .2 .1 5 0 0 ,
ion
1 9 8 .6 9 .1 0 .2 .1 5 0 0 } ect 2 0 6 .6 2 .2 2 6 .3 5 .2 1 }
nn
co
s e rv e r
( c h ild 2 )
c o n n e c te d
socket {2 0 6 .6 2 .2 2 6 .3 5 .2 1 ,
1 9 8 .6 9 .1 0 .2 .1 5 0 1 }
F ig u re 2 . 1 0 S e c o n d c lie n t c o n n e c t io n w it h s a m e s e r v e r