25
2.5 SOCKET-INTERFACE PROGRAMMING 2.5 SOCKET-INTERFACE PROGRAMMING 1 Computer Communications and Networks

Socket interface programming

Embed Size (px)

DESCRIPTION

Socket interface programming

Citation preview

Page 1: Socket interface programming

2.5 SOCKET-INTERFACE PROGRAMMING2.5 SOCKET-INTERFACE PROGRAMMING

1

Computer Communications and Networks

Page 2: Socket interface programming

2

Socket: End point of an inter-process communication flow across a computer network.

Different types of sockets :

Stream socket :(connection- oriented socket)

•It provides reliable, connected networking service

•Error free; no out- of- order packets (uses TCP)

•applications: telnet, http, …

Datagram socket :(connectionless socket)

•It provides unreliable, best- effort networking service

•Packets may be lost; may arrive out of order (uses UDP)

•applications: streaming audio/ video

2.5 SOCKET-INTERFACE PROGRAMMING2.5 SOCKET-INTERFACE PROGRAMMING

Page 3: Socket interface programming

2.5.1 Socket Interface in C

• In this section, we show how this interface is implemented in the C language.

• The important issue in socket interface is to understand the role of a socket in

communication.

• The socket has no buffer to store data to be sent or received.

• It is capable of neither sending nor receiving data. The socket just acts as a

reference or a label.

• The buffers and necessary variables are created inside the operating system.

3

Page 4: Socket interface programming

2.5.1 (continued)

Data Structure for SocketData Structure for Socket

Header FilesHeader Files

Sockets Used for UDPSockets Used for UDP Communication Flow DiagramCommunication Flow Diagram Programming ExamplesProgramming Examples

Communication Using UDPCommunication Using UDP

Communication Using TCPCommunication Using TCP

Sockets Used in TCPSockets Used in TCP Communication Flow DiagramCommunication Flow Diagram

4

Page 5: Socket interface programming

Figure 2.58: Socket data structure

5

C language defines a socket as a structure (strcut) and socket structure is made of 5 field .

Page 6: Socket interface programming

6

Family : Defines family protocol(how to interpret the addresses and port

number) ,PF_INET ,PF_INET6.

Type : Defines four types of sockets.

SOCK_STREAM (for TCP)

SOCK_DGRAM( for UDP)

SOCK_SEQPACKET (for SCTP)

SOCK_RAW( for application that directly use the services of IP)

Protocol : (defines specific protocol, set to 0 for TCP/IP)

Local Socket Address: Defines local socket address. Socket Address is itself made of the length

field , family field(set to the constant AF_INET for TCP/IP), port number(which defines the

process) and IP address field(which defines the host on which the process is running) . Also

contains an unused field.

Remote Socket Address: Define the remote socket address. Structure is the same as the local

socket address.

Page 7: Socket interface programming

The Socket Primitives

7

Page 8: Socket interface programming

Figure 2.59: Sockets for UDP communication

8

Page 9: Socket interface programming

Figure 2.60: Flow diagram for iterative UDP communication

9

Page 10: Socket interface programming

Table 2.22: Echo server program using UDP

10

Page 11: Socket interface programming

Table 2.22: Echo server program using UDP (continued)

11

Page 12: Socket interface programming

Table 2.23: Echo client program using UDP

12

Page 13: Socket interface programming

Table 2.23: Echo client program using UDP (continued)

13

Page 14: Socket interface programming

Table 2.23: Echo client program using UDP (continued)

14

Page 15: Socket interface programming

Figure 2.61: Sockets used in TCP communication

Create

2

Create5

15

Page 16: Socket interface programming

Figure 2.62: Flow diagram for iterative TCP communication

16

Page 17: Socket interface programming

Figure 2.63: Flow diagram for data-transfer boxes

17

Page 18: Socket interface programming

Figure 2.64: Buffer used for receiving

18

Page 19: Socket interface programming

Table 2.24: Echo server program using the services of TCP

2.19

19

Page 20: Socket interface programming

Table 2.24: TCP Echo server program(continued)

2.20

20

Page 21: Socket interface programming

Table 2.24: TCP Echo server program (continued)

2.21

21

Page 22: Socket interface programming

Table 2.25: Echo client program using the services of TCP

2.22

22

Page 23: Socket interface programming

Table 2.25: TCP Echo client program (continued)

23

Page 24: Socket interface programming

Table 2.24: TCP Echo client program(continued)

24

Page 25: Socket interface programming

25

Thank You….Thank You….