Академический Документы
Профессиональный Документы
Культура Документы
2nd Edition
Chapter 1. Introduction
Contents
Introduction A Simple Daytime Client Error Handling: Wrapper Functions A Simple Daytime Server OSI Model Unix Standards
1.1 Introduction
Client / Server
Client
Communication link
Server
Client ...
Client ... Client
Figure 1.2 Server handling multiple clients at the same time.
UNIX Network Programming 3
Server
Web Client
Application protocol
Web server
Application layer
TCP
transport layer
within kernel
IP
IP protocol
IP
network layer
Ethernet driver
Ethernet protocol
Actual flow between client and server
Ethernet driver
datalink layer
Ethernet Figure 1.3 Client and server on the same Ethernet communicating using TCP
UNIX Network Programming 4
LAN
router
router
router
router
Figure 1.4 Client and server on different LANs connected through a WAN
UNIX Network Programming 5
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) err_sys(socket error); bzero( &servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); /* daytime server */ if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) err_quit(inet_pton error for %s,argv[1]); if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0) err_sys(connect error); while ( (n = read(sockfd, recvline, MAXLINE)) > 0 ) { recvline[n] = 0; /* null termicate */ if ( fputs(recvline, stdout) == EOF) err_sys(fputs error); } if ( n < 0 ) err_sys ( read error ); exit(0); UNIX Network Programming }
3 4
if (argc != 2)
if ( (sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) err_sys("socket error"); 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]); if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0) err_sys("connect error"); while ( (n = read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = 0; /* null terminate */ if (fputs(recvline, stdout) == EOF) err_sys("fputs error"); } if (n < 0) err_sys("read error"); exit(0); }
1 2
listenfd = Socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13); /* daytime server */ Bind(listenfd, (SA *) *servaddr, sizeof(servaddr) );
ticks = time(NULL); snprintf(buff, sizeof(buff), %.24s\r\n, ctime(&ticks) ); Write(connfd, buff, strlen(buff) ); Close(connfd); } }
Application Sockets TCP | | UDP IPv4, IPv6 Device driver and Hardware Internet protocol suite
XTI
kernel communication details
OSI Model
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.
10
11
Contents
Introduction The Big Picture UDP: User Datagram Protocol TCP: Transmission Control Protocol TCP Connection Establishment and Termination TIME_WAIT State Port Numbers TCP Port Numbers and Concurrent Servers Buffer Sizes and Limitations
12
2.1 Introduction
Overview of the TCP / IP protocol Transport layer : TCP & UDP
13
API
TCP
UDP
ICMP
IGMP
IPv4
IPv6
ICMPv 6
14
15
server
socket,bind,listen accpet(blocks)
1 a ck J + S YN K ,
connect returns
a ck K + 1
TCP Header
0
16- bit source port number
15
16
16- bit destination port number
32
32- bit sequence number 32- bit acknowledgment number 4- bit header length reserved (6bit)
U R G A C K P S H R S T S Y N F I N
20 bytes
TCP Header
18
Encapsulation
user data application Appl header
user data
IP
IP header
Ethernet header 14
IP header 20
TCP header 20
Ethernet
19
20
server
FIN M
(passive close) read returns 0 close
1 a ck M +
connect returns
FIN N
a ck N + 1
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.
UNIX Network Programming 21
starting point
2.5 TCP
Connection Establishment and Termination
(cont)
s en N; SY d: N ,A SY CK T
CLOSED
en op ive N ct Y :a :S pl a p en d s
LISTEN
passive open
: ecv
S v: R rec
SYN_RCVD
recv: SYN
SYN_SENT
active open
CLOSE_WAIT
FIN_WAIT_1
CLOSING
LAST_ACK
re
cv
passive close
TIME_WAIT
2MSL timeout
active close
22
server
S YN J , m s s = 146 0
24 s s = 10 J+ 1,m N K , a ck SY
a ck K + 1
write read(blocks)
data(req uest)
read returns
write read(blocks)
ack of re ply
FIN M
CLOSE_WAIT (passive close) read returns 0 close LAST_ACK
1 a ck M +
FIN_WAIT_2 TIME_WAIT
FIN N
a ck N + 1
CLOSED
23
24
25
server
listening socket
(*.21, *.*)
Figure 2.8
206.62.226.35 206.62.226.66
server
listening socket
connected socket
26
server
listening socket
{198.69.10.2.1500, 206.62.226.35.21}
client2
ion ect
connected socket
nn co
{198.69.10.2.1500, 206.62.226.35.21}
connected socket
Figure 2.10
27
28
TCP output
29
UDP output
30
31
32