当前位置:文档之家› Unix Socket程序设计1

Unix Socket程序设计1

Analyst Meet
高级计算机网络技术
A August t 27, 27 2002
授课人 张伟哲 授课人:张伟哲 哈尔滨工业大学计算机学院 计算机网络与信息安全技术研究中心
SCST
HIT

UNIX Socket Programming (一 )
A August t 27, 27 2002
Analyst Meet
授课人 张伟哲 授课人:张伟哲 哈尔滨工业大学计算机学院 计算机网络与信息安全技术研究中心
SCST
HIT

提纲
概述 传输层:TCP和UDP 基本套接口编程
? ? ?
SCST
3
3
HIT

Introduction
■ Client
/ Server
Client
Communication link
Server
Figure 1.1 Network application : client and server
Client ... Client ... Client
Figure 1.2 Server handling multiple clients at the same time.
SCST
4
Server
4
HIT

Introduction (continued)
■ Example p
User process
: Client and Server on the same Ethernet communication using TCP
Web Client TCP Application protocol Web server TCP
A li ti layer Application l
TCP protocol
transport layer la er Protocol stack within kernel
IP Ethernet driver
IP protocol
IP Ethernet driver
network layer
Ethernet protocol
Actual flow between client and server
datalink layer
Ethernet Figure Ethernet communicating using TCP SCST 1.3 Client and server on the same 5 HIT
5

Introduction (continued)
■ Example p
: Client and Server on different LANs connected through WAN.
client li t application Host with TCP/IP LAN router WAN router
SCST
server application Host with TCP/IP LAN router
router
6
router
router
HIT
6
Figure 1.4 Client and server on different LANs connected through a WAN

A Simple Daytime Client
1
#include “unp.h” int main (int argc, char **argv) { int sockfd, n; char recvline[MAXLINE+1]; struct sockaddr_in servaddr; if ( argc != 2) err_quit(“usage: a.out ”);
2 3
if ( (sockfd ( kfd = socket(AF_INET, k t(AF INET SOCK SOCK_STREAM, STREAM 0)) < 0) err_sys(“socket error”); bzero( &servaddr &servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); /* daytime server */ (inet_p pton(AF ( _INET, , argv[1], g [ ], &servaddr.sin_addr) ) <= 0) ) if ( err_quit(“inet_pton error for %s”,argv[1]);
SCST 7 HIT
7

A Simple Daytime Client
4
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 y( p error”); ); } if ( n < 0 ) err_sys ( “read error “); exit(0); }
SCST 8 HIT
5
8

protocol Independence
■ ■ ■ ■ ■ ■
#include "unp.h" int main(int argc, char **argv) { int struct sockaddr_in6 char if (argc ! != 2)
sockfd, n; servaddr; recvline[MAXLINE + 1]; err quit("usage: err_quit( usage: a.out "); IPaddress );

■ ■
if ( (sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) err_sys("socket error"); bzero(&servaddr, b ero(&ser addr sizeof(servaddr)); si eof(ser addr)); 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");
■ ■ ■ ■ ■
■ ■
■ ■ ■ ■ ■ ■ ■
SCST

exit(0);
9
HIT
9■
}

Error Handling: Wrapper Functions

Since terminating on an error is the common case, we can shorten our program by defining a wrapper function that performs the actual function call, tests the return value, and terminates on an error.
sockfd kfd = S Socket(AF_INET, k (AF INET SOCK SOCK_STREAM, STREAM 0) 0);
int Socket(int family, int type, int protocol) { int n; if( (n = socket( family, type, protocol)) < 0 ) err_sys(“socket error”); return (n); }

Figure 1.7 Our wrapper function for the socket function

Unix errno Value
SCST 10 HIT
10

A Simple Daytime Server
#include “unp.h” #include int main(int argc, char **argv) { int listenfd, connfd; struct sockaddr_in servaddr; char buff[MAXLINE]; time_t ticks;
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, Bind(listenfd, *servaddr sizeof(servaddr) );
SCST 11 HIT
11

A Simple Daytime Server
3
Listen(listenfd, LISTENQ); for( ; ; ) { connfd = Accept(listenfd, (SA *) ) NULL, NULL) ; ticks = time(NULL); snprintf(buff, sizeof(buff), “%.24s\r\n”, ctime(&ticks) ); Write(connfd buff Write(connfd, buff, strlen(buff) ); Close(connfd); ( ); } }
SCST 12 HIT
4
12

OSI Model
7 Application 6 Presentation 5 Session 4 Transport 3 N t Network k 2 Datalink 1 Physical
Application TCP | | UDP IP 4 IP IPv4, IPv6 6 Device driver and Hardware Internet protocol suite
application details user p process
Sockets
XTI
kernel communication details
OSI Model
Figure g 1.14 Layers y on OSI model and Internet p 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, Fi the h upper three h layers l handle h dl all ll the h d details il of f the h application li i and d The Th 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.
SCST 13 HIT
13

Unix Standards

POSIX
■ ■
Potable Operating System Interface a family of standards being developed by IEEE an international consortium of vendors and end-user end user customers from industry, government, and academia. a large open international community of network designers, g , operators, p , vendors, , and researchers concerned with the evolution of the Internet architecture and the smooth operation of the internet.
SCST 14 HIT

The Open Group


IETF (Internet Engineering Task Force)

14

提纲
概述 传输层:TCP和UDP 基本套接口编程
? ? ?
SCST
15
15
HIT

The Big Picture
I P v4 a p p l i c ati on A F _I N ET so cka d d r_i n{ }
tcp d um p m ro uted pi ng trace tracero ute ap p l . ap p l . ap p l .
I P v6 ap p l i ca ti on A F _I N E T6 so c ka d d r_i n 6{ }
ap p l . trace tracero ute pi ng
API
TC P
UDP
I CM P
I GM P
I P v4
32- b i t ad d resses
128- b i t ad d resses
I P v6
I C M Pv 6
ARP, RARP B PF, D LP I d atal i nk
Fi g u re 2 .1 1 O ve rvi i e w o f T C P /I P p ro to c o l s
SCST
16
16
HIT

UDP : User Datagram Protocol

The application writes a datagram to a UDP socket, which is encapsulated as either a IPv4 of a IPv6 datagram, which is sent to its destination. UDP provides a connectionless service. Each UDP datagram has a length and we can g as a record. consider a datagram RFC 768[Postel 1980]
SCST 17 HIT
■ ■

17

TCP: Transmission Control Protocol
■ ■
Provides connections between clients and servers. Provides reliability.

RTT ( round-trip-time)

TCP also sequences the data by associating a sequence number with every byte that it sends. TCP provides flow control.


window

A TCP connection is also full-duplex.
SCST 18 HIT
18

TCP Connection Establishment
and Termination

Three-Way Handshake
cl i en t
socket connect t(b l o cks) (acti o n o p en )
SYN J
server
socket,bind,listen accpet(b l o cks)
1 ack J + SYN K,
connect return s
ack K + 1
accept return s read(b l o cks)
Fi g u re 2.2 TC P three- w ay han d shake
■ ■
19
SYN segment g ACK SCST
19
HIT

TCP Header
0
16- b i t so urce p o rt n um b er
15
16
16- b i t d esti n ati o n p o rt n um b er
32
32- b i t seq uen ce n um b er 32- b i t ackn o w l ed g m en t n um b er 4- bi t header l en g th reserved (6b i t)
U R G A C K P S H R S T S Y N F I N
20 b ytes
16- b i tw i n d o w si ze 16- b i t urg en t p o i n ter
16- b i t TC P checksum o p ti o n (i f an y)
d ata (i f an y)
TC P H ead er
SCST
20
20
HIT

相关主题
文本预览
相关文档 最新文档