1. TCP/IP Reference Model
To achieve the interconnection of various networks, the International Organization for Standardization (ISO) has developed an Open System Interconnection (OSI) Reference Model. Although the OSI architecture is theoretically complete, commercial products that fully comply with OSI protocols seldom enter the market. However, the use of TCP/IP protocols has flood into the market. Almost all workstations are equipped with TCP/IP protocols, making TCP/IP a practical international standard for computer networks.
2. socket)
Socket is an important part of the operating system. It is the foundation of network applications. In terms of hierarchy, it is located at the application layer and is an API provided by the operating system for application programmers. Through it, applications can access the transport layer protocol.
1. the socket is located above the transport layer protocol, shielding the differences between different network protocols;
2. socket is the entry to network programming. It provides a large number of system calls and forms the main body of network programs;
3. in Linux, sockets are part of the file system. network communication can be seen as reading files, making network control as convenient as file control.
2.1 socket address
On the transport layer, the communication endpoint can be described by three parameters on the Internet: the protocol, IP address, and port number used. These contents are described by sockaddr: // Usr/include/sys/socket. h
Typedef unsigned short sa_family_t;
// General socket address
Struct sockaddr {
Sa_family_t sa_family;/* address family, AF_xxx, protocol cluster */
Char sa_data [14];/* 14 bytes of protocol address */
};
// Usr/include/netinet/in. h
// The socket address of the INET address cluster
Struct in_addr {
_ U32 s_addr;
};
Struct sockaddr_in {
Sa_family_t sin_family;/* Address family: AF_INET */
Unsigned short int sin_port;/* Port number, Port */
Struct in_addr sin_addr;/* Internet address, IP address */
/* Pad to size of 'struct sockadd '.*/
Unsigned char sin_zero [sizeof (struct sockaddr )-
Sizeof (sa_family_t )-
Sizeof (uint16_t )-
Sizeof (struct in_addr)];
};
Socket address families supported by Linux:
|
Socket address family |
Description |
|
UNIX |
UNIX domain socket |
|
INET |
Internet address families supported by TCP/IP protocol |
|
AX25 |
Amater radio X25 |
|
APPLETALK |
Appletalk DDP |
|
IPX |
Novell IPX |
|
X25 |
X25 |
Supported BSD socket types in Linux:
|
BSD socket type |
Description |
|
Stream) |
This socket provides a reliable bidirectional ordered data stream, which ensures that data is not lost, damaged, or repeat during transmission. Stream sockets are implemented through the TCP protocol of the INET address family. |
|
Datagram) |
This socket also provides two-way data transmission, but does not guarantee data transmission. That is to say, data may be transmitted in wrong order, or even lost or damaged. This type of socket is implemented through the UDP protocol of the INET address family. |
|
Raw) |
With this type of socket, the process can directly access the underlying protocol (thus called the original ). For example, you can open the original socket on an Ethernet device and obtain the original IP data transmission information. |
|
Reliable messages |
It is similar to the datagram socket, but ensures that the data is correctly transmitted to the target end. |
|
Ordered data packets |
Similar to a stream socket, but the packet size is fixed. |
|
Packet) |
This is not a standard BSD socket type. It is a proprietary BSD socket extension in Linux that allows processes to directly access data packets at the device level. |
2.2 socket operations
Sockets (BSD socket) provide basic APIs for applications. These APIs are the basis for compiling network applications.