Docsity

Lecture Slides on Socket Interface - Computer Networks | COSC 6377, Study notes of Computer Systems Networking and Telecommunications

Partial preview of the text

Download Lecture Slides on Socket Interface - Computer Networks | COSC 6377 and more Study notes Computer Systems Networking and Telecommunications in PDF only on Docsity!

Forouzan Notes COSC 6377 - Fall 2000 24-

Chapter 24

Socket Interface

  • Sockets
  • Socket System Calls
  • Connectionless Iterative Server
  • UDP Client-Server Programs
  • Connection-Oriented Concurrent Server
  • TCP Client-Server Programs

Forouzan Notes COSC 6377 - Fall 2000 24-

u_char Unsigned 8-bit character

u_short Unsigned 16-bit integer

u_long Unsigned 32-bit integer

Figure 24-1 (^) Data types

Forouzan Notes COSC 6377 - Fall 2000 24-

struct in_addr

u_long s_addr ;

s_addr

in_addr

Figure 24-2 (^) Internet address structure

Forouzan Notes COSC 6377 - Fall 2000 24-

struct sockaddr_in

u_char

u_short

u_short

struct in_addr

char

sin_len sin_family sin_port sin_addr sin_zero

sockaddr_in

...

sin_len ;

sin_family ;

sin_port ;

sin_addr ;

sin_zero [8]^ ;

Figure 24-3 (^) Socket address structure

Forouzan Notes COSC 6377 - Fall 2000 24-

Data

MS byte LS byte

Memory

00001010 A
A + 1
A + 2
A + 3

Figure 24-6 (^) Big-endian byte order

Forouzan Notes COSC 6377 - Fall 2000 24-

Data

Memory

A

MS byte LS byte

Figure 24-7 (^) Little-endian byte order

A + 1
A + 2
A + 3

Forouzan Notes COSC 6377 - Fall 2000 24-

Host byte order

Network byte order

htons ntohs

16-bit

16-bit

32-bit

32-bit

htonl ntohl

Figure 24-8 (^) Byte-order transformation functions

Forouzan Notes COSC 6377 - Fall 2000 24-

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 ) ;

Figure 24-9 (^) Declarations for byte-order transformation

Forouzan Notes COSC 6377 - Fall 2000 24-

Figure 24-12 (^) Declaration for byte-manipulation functions

void memset ( void _dest_ , int chr , int len ) ;

void memcpy ( void _dest_ , const void _src_* , int len ) ;

int memcmp ( const void _first_* , const void _second_* , int len ) ;

Forouzan Notes COSC 6377 - Fall 2000 24-

struct hostent *gethostbyname ( const char *hostname ) ;

Figure 24-13 (^) Declaration for gethostbyname

Forouzan Notes COSC 6377 - Fall 2000 24-

alias

struct hostent

char *h_name ;

char **h_aliases ;

int h_addrtype ;

int h_length ;

char **h_addr_list ;

h_aliases h_addrtype h_length h_addr_list

AF_INET 4

alias

h_name

hostent

. . .

address address . . .

Figure 24-14 (^) The hostent structure

name

Forouzan Notes COSC 6377 - Fall 2000 24-

int socket (int family , int type , int protocol ) ;

AF_INET SOCK_DGRAM
SOCK_STREAM

Returns a socket descriptor if successful;−1 if error.

Figure 24-15 (^) Declaration for socket function

Forouzan Notes COSC 6377 - Fall 2000 24-

int listen (int sockfd , int backlog ) ;

Returns 0 if successful; - 1 if error

Figure 24-18 (^) Declaration for listen function

Forouzan Notes COSC 6377 - Fall 2000 24-

int accept (int (^) sockfd , const struct sockaddr_in (^) _clientaddr_* , int (^) _clientaddrlen_* ) ;

Returns a socket descriptor if successful; - 1 if error

Figure 24-19 (^) Declaration for accept function

Forouzan Notes COSC 6377 - Fall 2000 24-

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

Figure 24-20 (^) Declaration for sendto function

Forouzan Notes COSC 6377 - Fall 2000 24-

int recvfrom (int (^) sockfd , const void (^) _buf_* , int (^) buflen , int (^) flags const struct sockaddr_in (^) _fromaddr_* , int (^) _fromaddrlen_* ) ;

Figure 24-21 (^) Declaration for recvfrom function

Returns number of bytes sent if successful; - 1 if error

Forouzan Notes COSC 6377 - Fall 2000 24-

int close (int sockfd ) ;

Returns 0 if successful;−1 if error.

Figure 24-24 (^) Declaration for close function

Forouzan Notes COSC 6377 - Fall 2000 24-

sendto (...)

socket (...)

bind (...)

Process

Repeat infinitely

recvfrom (...)

Server

Requests

Responses

socket (...)

sendto (...)

close (...)

Repeat as needed

recvfrom (...)

Clients

Each server serves many clients but handles one request at a time. Requests from different clients can be mingled together.

Figure 24-25 Socket interface for connectionless iterative server

Forouzan Notes COSC 6377 - Fall 2000 24-

socket (...) connect (...)

write (...)

close (...)

as neededRepeat

socket (...)

bind (...)

listen (...)

accept (...)

fork (...)

read (...)

write (...)

(listening)close

Process

Repeat infinitely

as neededRepeat

read (...)

Connection request

Parent server

Child server

request

response

Server

Client

each child server serves only one client.A parent server creates many children;

(accepting)close

(accepting)close

Figure 24-26 Socket interface for connection-oriented concurrent server

Forouzan Notes COSC 6377 - Fall 2000 24-

Client Server

a. After connect, before accept

b. After accept

c. After fork

d. After parent closes ephemeral port

e. After child closes well-known port

Parent

Parent

Parent

Parent

Parent

Child

Child

Child

Figure 24-27 Relationship between the client and the server