socket interface

43
©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl , 2003 1 Chapter 16 Socket Interface

Upload: veda-vyas

Post on 20-Oct-2015

23 views

Category:

Documents


0 download

DESCRIPTION

aca

TRANSCRIPT

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 200311

1

Chapter 16

SocketInterface

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 200322

2

CONTENTSCONTENTS• SOME DEFINITIONS• SOCKETS• BYTE ORDERING• ADDRESS TRANSFORMATION• BYTE MANIPULATION FUNCTIONS• INFORMATION ABOUT REMOTE HOST• SOCKET SYSTEM CALLS• CONNECTIONLESS ITERATIVE SERVER

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 200333

3

CONTENTS (continued)CONTENTS (continued)• UDP CLIENT-SERVER PROGRAMS• CONNECTION-ORIENTED CONCURRENT SERVER• TCP CLIENT-SERVER PROGRAMS

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 200344

4

SOME DEFINITIONS

16.116.1

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 200355

5

Figure 16-1

Data types

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 200366

6

Figure 16-2

Internet address structure

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 200377

7

SOCKETS

16.216.2

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 200388

8

Figure 16-3

Socket address

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 200399

9

Figure 16-4

Socket structure

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20031010

10

Figure 16-5

Socket types

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20031111

11

BYTE ORDERING

16.316.3

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20031212

12

Figure 16-6

Big-endian byte order

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20031313

13

Figure 16-7

Little-endian byte order

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20031414

14

The byte order for the TCP/IPThe byte order for the TCP/IPprotocol suite is big endian.protocol suite is big endian.

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20031515

15

Figure 16-8

Byte-order transformation

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20031616

16

Figure 16-9

Declarations for byte-order transformation

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20031717

17

ADDRESSTRANSFORMATION

16.416.4

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20031818

18

Figure 16-10

Address transformation

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20031919

19

Figure 16-11

Declarations for address transformation

functions

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20032020

20

BYTEMANIPULATION

FUNCTIONS

16.516.5

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20032121

21

Figure 16-12

Declarations for byte-manipulation functions

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20032222

22

INFORMATIONABOUT REMOTE

HOST

16.616.6

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20032323

23

Figure 16-14

Hostent structure

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20032424

24

SOCKET SYSTEMCALLS

16.716.7

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20032525

25

Figure 16-15

Declaration for socket function

Create a blank socket.

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20032626

26

Figure 16-16

Declaration for bind function

Server Application binds a socket to a given socket address.

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20032727

27

Figure 16-17

Declaration for connect function

Client Application actively opens a connection with a remote server socket

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20032828

28

Figure 16-18

Declaration for listen function

Server Application is ready to receive connection requests

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20032929

29

Figure 16-19

Declaration for accept function

Server Application accepts the next connection request. A new “child” socket is created and initialized by the Operating System. Its socket descriptor is the “return value”

If no requests pending, sleep.

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20033030

30

Figure 16-20

Declaration for sendto function

Application sends UDP data to a remote socket

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20033131

31

Figure 16-21

Declaration for recvfrom function

Get the next message arriving at a socket, as well as the socket address of the sender.

Usually used by a UDP process

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20033232

32

Figure 16-22

Declaration for read function

Read the next message arriving at a specific Stream (i.e. TCP) socket.

Assumes that the connection has already been opened.

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20033333

33

Figure 16-23

Declaration for write function

Send a message to a specific Stream (i.e. TCP) socket. Assumes that the connection has already been opened.

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20033434

34

Figure 16-24

Declaration for close function

Terminate the TCP connection

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20033535

35

CONNECTIONLESSITERATIVE

SERVER

16.816.8

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20033636

36

Socket interfacefor connectionless

iterative server

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20033737

37

UDP CLIENT-SERVERPROGRAMS

16.916.9

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20033838

38

CONNECTION-ORIENTEDCONCURRENT SERVER

16.1016.10

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20033939

39

Figure 16-26

Socket interfacefor connection-oriented

concurrent server

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20034040

40

Figure 16-26 (repeated), Part I

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20034141

41

Figure 16-26 (repeated), Part II

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20034242

42

Figure 16-27, Part I

Clientand

Server

©The McGraw-Hill Companies, Inc., 2000 © Adapted for use at JMU by Mohamed Aboutabl, 20034343

43

Figure 16-27, Part II