Вы находитесь на странице: 1из 25

Computer Communications and Networks

2.5SOCKETINTERFACEPROGRAMMING

2.5SOCKETINTERFACEPROGRAMMING
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.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.

2.5.1 (continued)

Data Structure for Socket

Header Files

Communication Using UDP

Sockets Used for UDP


Communication Flow Diagram
Programming Examples

Communication Using TCP

Sockets Used in TCP


Communication Flow Diagram
4

Figure 2.58: Socket data structure


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

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.

The Socket Primitives

Figure 2.59: Sockets for UDP communication

Figure 2.60: Flow diagram for iterative UDP communication

Table 2.22: Echo server program using UDP

10

Table 2.22: Echo server program using UDP (continued)

11

Table 2.23: Echo client program using UDP

12

Table 2.23: Echo client program using UDP (continued)

13

Table 2.23: Echo client program using UDP (continued)

14

Figure 2.61: Sockets used in TCP communication

2
Create

5
Create

15

Figure 2.62: Flow diagram for iterative TCP communication

16

Figure 2.63: Flow diagram for data-transfer boxes

17

Figure 2.64: Buffer used for receiving

18

Table 2.24: Echo server program using the services of TCP

19
2.19

Table 2.24: TCP Echo server program(continued)

20
2.20

Table 2.24: TCP Echo server program (continued)

21
2.21

Table 2.25: Echo client program using the services of TCP

22
2.22

Table 2.25: TCP Echo client program (continued)

23

Table 2.24: TCP Echo client program(continued)

24

ThankYou.

25

Вам также может понравиться