TCP-IP socket programming for network programming (1)
To communicate between two network programs, you need to establish a connection channel between them, which is composed of a quintuple.
Quintuple: (protocol, local IP address, local port number, remote IP address, remote port number)
Socket programming principles
Socket set Interface
Communication between network programs relies on socket interfaces.
For example, if the QQ program on host A needs to send A message to host B, the steps are as follows:
① The QQ messages on host A are sent to the socket on host.
② This socket is transmitted to the network interface card in host B through the network management software of host.
③ The network interface card of host B receives the message and then transmits it to the network management software of host B and then to the socket of host B.
④ Then, QQ in host B reads the information in socket.
Set interface category
① Stream set interface (SOCK_STREAM) reliable connection-oriented data transmission service (applicable to TCP)
② The SOCK_DGRAM interface does not have a two-way Data Transmission Service (using UDP) with unreliable connection)
③ The original interface (SOCK_RAM) is rarely used
Socket programming principles
① When the server calls the socket () function to start a service, it creates an interface set.
② Use the bind () function to bind a port to the socket.
③ Call the listen () function to listen to client requests
④ The client also calls the socket () function to wear a set of interfaces requesting services, and then calls the connect () function to request connections.
⑤ The server calls the accept () function to receive request connections, then reads data from the set interface using the recv () function, and calls the send () function to send data to the set Interface
6. Disable the set of interface resources