Slides from TCP/IP - Forouzan
Chapter 24
Socket Interface Sockets • Socket System Calls • Connectionless Iterative Serv S erver er • UDP Client-Server Programs • Connection-Oriented Concurrent Server • TCP Client-Server Programs
Forouzan Notes
COSC 6377 - Fall 2000
Figure 24-1
Forouzan Notes
Forouzan Notes
24-1
Data types
u_char
Unsigned 8-bit character
u_short
Unsigned 16-bit integer
u_long
Unsigned 32-bit integer
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-2
24-1
Slides from TCP/IP - Forouzan
Figure 24-2
Internet address structure
s_addr in_addr
struct {
in_addr u_long s_addr ;
};
Forouzan Notes
COSC 6377 - Fall 2000
Figure 24-3
24-3
Socket address structure
... sin_len
sin_family
sin_port
sin_addr
sin_zero
sockaddr_in
struct {
sockaddr_in u_char u_short u_short struct in_addr char
sin_len ; sin_family ; sin_port ; sin_addr ; sin_zero [8] ;
};
Forouzan Notes
Forouzan Notes
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-4
24-2
Slides from TCP/IP - Forouzan
Figure 24-4
Socket structure
Family
Type
Protocol
... sin_len
sin_family
sin_port
sin_addr
sin_zero
Local Socket Address
... sin_len
sin_family
sin_port
sin_addr
sin_zero
Remote Socket Address Socket
Forouzan Notes
COSC 6377 - Fall 2000
Figure 24-5
24-5
Socket types
Application program Datagram socket interface
Stream socket interface TCP
Raw socket interface UDP IP
Physical and data link layers
Forouzan Notes
Forouzan Notes
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-6
24-3
Slides from TCP/IP - Forouzan
Figure 24-6
Big-endian byte order
MS byte 00001010
Data
00010111
00001110
LS byte 00000110
Memory
Forouzan Notes
Data
A
00010111
A+1
00001110
A+2
00000110
A+3
COSC 6377 - Fall 2000
Figure 24-7
MS byte 00001010
00001010
24-7
Little-endian byte order
00010111
00001110
LS byte 00000110
Memory
Forouzan Notes
Forouzan Notes
00000110
A
00001110
A+1
00010111
A+2
00001010
A+3
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-8
24-
Slides from TCP/IP - Forouzan
Figure 24-8
Byte-order transformation functions
Host byte order 16-bit
htons
32-bit
ntohs
htonl
16-bit
ntohl
32-bit Network byte order
Forouzan Notes
COSC 6377 - Fall 2000
Figure 24-9
Forouzan Notes
Declarations for byte-order transformation
u_short
htons ( u_short
host_short ) ;
u_short
ntohs ( u_short
network_short ) ;
u_long
htonl ( u_long
host_long ) ;
u_long
ntohl ( u_long
network_long ) ;
Forouzan Notes
24-9
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-10
24-5
Slides from TCP/IP - Forouzan
Figure 24-10
Address transformation transformation
32-bit binary address
inet_ntoa
inet_aton
Dotted decimal address
Forouzan Notes
Figure 24-11
int char
Forouzan Notes
Forouzan Notes
COSC 6377 - Fall 2000
24-11
Declarations for transformation functions
inet_aton ( const char *strptr , struct in_addr *addrptr ) ; *inet_ntoa (struct in_addr inaddr ) ;
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-12
24-6
Slides from TCP/IP - Forouzan
Figure 24-12
Declaration for byte-manipulation functions
void *memset
( void *dest , int chr , int len ) ;
void *memcpy
( void *dest , const void
int
Forouzan Notes
*src , int len ) ;
memcmp ( const void *first , const void *second , int len ) ;
COSC 6377 - Fall 2000
Figure 24-13
24-13
Declaration for gethostbyname
struct hostent *gethostbyname ( const char *hostname ) ;
Forouzan Notes
Forouzan Notes
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-14
24-7
Slides from TCP/IP - Forouzan
Figure 24-14
The hostent structure alias alias
.. .
h_name
h_aliases
address address
.. .
AF_INET
4
h_addrtype
h_length
h_addr_list
hostent struct {
hostent char char int int char
*h_name ; **h_aliases ; h_addrtype ; h_length ; **h_addr_list ;
}; Forouzan Notes
COSC 6377 - Fall 2000
Figure 24-15
24-15
Declaration for socket function
int socket (int family , int type , int protocol ) ; AF_I AF_INE NET T SOCK_ OCK_DG DGRA RAM M SOCK_STREAM
0
Returns a socket descriptor if successful; − 1 if error.
Forouzan Notes
Forouzan Notes
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-16
24-8
Slides from TCP/IP - Forouzan
Figure 24-16
Declaration for bind function
int bind (int sockfd , const struct sockaddr_in *localaddr , int localaddrlen) ; Returns 0 if successful; successful; −1 if error.
Forouzan Notes
COSC 6377 - Fall 2000
Figure 24-17
24-17
Declaration for connect function
int connect (int sockfd , const struct sockaddr_in *serveraddr , int serveraddrlen ) ; Returns 0 if successful; - 1 if error
Forouzan Notes
Forouzan Notes
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-18
24-9
Slides from TCP/IP - Forouzan
Figure 24-18
Declaration for listen function
int listen (int sockfd , int backlog ) ; Returns 0 if successful; - 1 if error
Forouzan Notes
COSC 6377 - Fall 2000
Figure 24-19
24-19
Declaration for accept function
int accept (int sockfd , const struct sockaddr_in *clientaddr , int *clientaddrlen) ; Returns a socket descriptor if successful; - 1 if error
Forouzan Notes
Forouzan Notes
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-20
24-1
Slides from TCP/IP - Forouzan
Figure 24-20
Declaration for sendto function
int sendto (int sockfd , const void *buf , int buflen , int flags , const struct sockaddr_in *toaddr , int toaddrlen ) ; Returns number of bytes sent if successful; - 1 if error
Forouzan Notes
COSC 6377 - Fall 2000
Figure 24-21
24-21
Declaration for recvfrom function
int recvfrom (int sockfd , const void *buf , int buflen , int flags const struct sockaddr_in *fromaddr , int *fromaddrlen ) ; Returns number of bytes sent if successful; - 1 if error
Forouzan Notes
Forouzan Notes
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-22
24-1
Slides from TCP/IP - Forouzan
Figure 24-22
Declaration for read function
int read (int sockfd , const void *buf , int buflen ) ; Returns number of bytes read if successful; 0 for end of file;− 1 if error.
Forouzan Notes
COSC 6377 - Fall 2000
Figure 24-23
24-23
Declaration for write function
int write (int sockfd , const void *buf , int buflen ) ; Returns number of bytes written if successful;− 1 if e rror.
Forouzan Notes
Forouzan Notes
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-24
24-1
Slides from TCP/IP - Forouzan
Figure 24-24
Declaration for close function
int close (int sockfd ) ; Returns 0 if successful;− 1 if error err or.
Forouzan Notes
COSC 6377 - Fall 2000
24-25
Socket interface for connectionless iterative server
Figure 24-25
Each server serves many clients but handles one request at a time. Requests from different clients can be mingled together.
Server socket (...)
Clients
bind (...)
socket (...)
Repeat as needed sendto (...)
Requests
Repeat infinitely
recvfrom (...) Process sendto (...) Responses recvfrom (...)
close (...) Forouzan Notes
Forouzan Notes
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-26
24-1
Slides from TCP/IP - Forouzan
Socket interface for connection-oriented concurrent server
Figure 24-26
Server socket (...) A parent server creates many children; each child server serves only one client.
bind (...)
listen (...) Client socket (...)
Repeat infinitely Connection request
connect (...) accept (...)
fork (...)
close (listening) Repeat as needed
close (accepting)
Parent server
write (...)
Repeat as needed request read (...) Process write (...) response
read (...)
close (accepting) Child server
close (...)
Forouzan Notes
COSC 6377 - Fall 2000
24-27
Relationship between the client and the server
Figure 24-27 Client
Ser ver Parent
a. After connect, before accept Parent
b. After accept Parent
Child
Parent
Child
c. After fork
d. After parent closes ephemeral port Parent
Child
e. After child closes well-known port Forouzan Notes
Forouzan Notes
COSC 6377 - Fall 2000
COSC 6377 - Fall 2000
24-28
24-1