Network programming - socket definition and address format
Table of contents
What is socket?
Socket address format
1. What is a socket?
In network programming, socket is translated as socket or socket interface, which means that the network connection and data transmission and reception can be quickly completed through the socket access method.
The figure above shows the core logic of client and server work in network programming.
On the server side, before the client initiates a connection request, the server side must be initialized.
Initialize the socket.
Execute the bind function to bind the service capability to a known address and port.
Then execute the listen operation to convert the original socket into a socket on the server side.
The server finally blocks on accept and waits for the client request.
When the server is ready, the client needs to initialize the socket first, and then execute connect to initiate a connection request to the address and port of the server, where the address and port must be known by the client in advance. The connect request is the TCP three-way handshake (Three-way Handshake).
After the three-way handshake is completed, the client and server establish a connection and enter the data transmission process.
The client process initiates a write byte stream operation to the operating system kernel, the kernel protocol stack transmits the byte stream to the server through the network device, the server gets information from the kernel, reads the byte stream from the kernel into the process, and Start the processing of business logic, and after completion, the server will write the obtained results to the client in the same way.
So once the connection is established, the transmission of data is no longer one-way, but two-way .
When the client completes the interaction with the server, such as performing a Telnet operation, or an HTTP request, and needs to disconnect from the server, the close function will be executed.
At this time, the operating system kernel will send a FIN packet to the server through the original connection link, and the server will perform a passive shutdown after receiving it. At this time, the entire link is in a half-closed state.
After that, the server will also execute the close function, and the entire link will be truly closed.
In the semi-closed state, the party that initiates the close request considers the connection to be normal until it receives the FIN packet from the other party.
In the fully closed state, both parties perceive that the connection has been closed.
All the above operations are done through socket. Whether it is the client's connect, or the server's accept, or read/write operations, etc., socket is the only way to establish a connection and transmit data .
2. Socket address format
When using sockets, the addressing problem of both communication parties must be solved first. The address of the socket is required to establish a connection. The format of the address of the socket is as follows.
The first field is the address family, which indicates how to interpret and save the address. There are many definitions of address families in glibc, the commonly used ones are as follows:
AF_LOCAL: Indicates the local address, corresponding to the Unix socket, generally used for local socket communication, and can also be written as AF_UNIX, AF_FILE.
AF_INET: IPv4 address used by the Internet.
AF_INET6: IPv6 address used by the Internet.
AF_ means Address Family, and PF_ means Protocol Family protocol family, such as PF_INET, PF_INET6, etc. Use the value of AF_xxx to initialize the socket address, and use the value of PF_xxx to initialize the socket. As you can see in the <sys/socket.h> header file, these two values are in one-to-one correspondence.
Same as sockaddr, there is a 16-bit sin_family field, the value is AF_INET for IPv4.
Port number, the port number is up to 16-bit, that is, the maximum supported 2 to the 16th power (65535).
Reserved ports are established by convention, and have been used by corresponding services, such as port 21 of ftp, port 22 of ssh, port 80 of http, etc.
Generally, ports larger than 5000 can be used as ports of your own application.
The following are reserved ports defined by glibc.
/* Standard well-known ports. */enum{
IPPORT_ECHO =7,/* Echo service. */
IPPORT_DISCARD =9,/* Discard transmissions service. */
IPPORT_SYSTAT =11,/* System status service. */
IPPORT_DAYTIME =13,/* Time of day service. */
IPPORT_NETSTAT =15,/* Network status service. */
IPPORT_FTP =21,/* File Transfer Protocol. */
IPPORT_TELNET =23,/* Telnet protocol. */
IPPORT_SMTP =25,/* Simple Mail Transfer Protocol. */
IPPORT_TIMESERVER =37,/* Timeserver service. */
IPPORT_NAMESERVER =42,/* Domain Name Service. */
IPPORT_WHOIS =43,/* Internet Whois service. */
IPPORT_MTP =57,
IPPORT_TFTP =69,/* Trivial File Transfer Protocol. */
IPPORT_RJE =77,
IPPORT_FINGER =79,/* Finger service. */
IPPORT_TTYLINK =87,
IPPORT_SUPDUP =95,/* SUPDUP protocol. */
IPPORT_EXECSERVER =512,/* execd service. */
IPPORT_LOGINSERVER =513,/* rlogind service. */
IPPORT_CMDSERVER =514,
IPPORT_EFSSERVER =520,/* UDP ports. */
IPPORT_BIFFUDP =512,
IPPORT_WHOSERVER =513,
IPPORT_ROUTESERVER =520,/* Ports less than this value are reserved for privileged processes. */
IPPORT_RESERVED =1024,/* Ports greater this value are reserved for (non-privileged) servers. */
IPPORT_USERRESERVED =5000
An IPv4 address is a 32-bit field, and the maximum number of supported addresses is 2 to the 32nd power, which is about 4.2 billion.
The length of the entire structure is 28 bytes, of which flow control information and domain IP do not appear on the official website of glibc, and the other is a currently unused field. The address family is AF_INET6, the port is the same as the IPv4 address, and the key address is upgraded from 32 bits to 128 bits, which completely solves the problem of insufficient addressing numbers.
Both IPv4 and IPv6 address formats are in the format of Internet sockets, and there is also a local socket format used for communication between local processes, namely AF_LOCAL.
The comparison of several addresses is shown in the figure below. The length of the IPv4 and IPv6 socket address structures is fixed, while the length of the local address structure is variable.