Network programming - Socket (socket)

network programming

    The purpose of network programming is to communicate directly or indirectly with other computers through network protocols. There are two main problems in network programming 
, one is how to accurately locate one or more hosts on the network, and the other is 
how to transmit data reliably and efficiently after finding the host. In the TCP/IP protocol, the IP layer is mainly responsible for the location of network hosts and the 
routing of data transmission. An IP address can uniquely determine a host on the Internet. The TCP layer provides an application-oriented reliable 
or unreliable data transmission mechanism, which is the main object of network programming, and generally does not need to care about how 
the IP layer handles data.

    The more popular network programming model is the client/server (C/S) structure. That is, one of the two parties in the communication acts as a 
server and waits for the client to make a request and respond. Clients apply to the server when they need services. The server generally 
runs as a daemon process all the time, listening to the network port. Once there is a client request, it will start a service process to respond to the 
client , and at the same time continue to monitor the service port, so that subsequent clients can also receive services in time.

    There is a one-to-one correspondence between IP addresses and host names on the Internet. Through domain name resolution, the IP of the machine can be obtained from the host name. 
Because the machine name is closer to natural language and easier to remember, it is more widely used than the IP address, but only for the machine. An IP 
address is a valid identifier.

    Usually, there are always many processes on a host that need network resources for network communication. The object of network communication is 
not exactly the host, but the process running in the host.
At this time, it is obviously not enough to have hostnames or IP addresses to identify so many processes  . The port number is a means to provide more network resources on a host, and it is also 
a mechanism provided by the TCP layer. Only through the combination of hostname or IP address and port number can the object in network communication be uniquely identified: 
process.

socket

    The so-called socket is usually also called "socket", which is used to describe the IP address and port, and is the handle of a communication chain.
Applications  typically make and respond to network requests through "sockets".

    Sockets can be classified according to the nature of the communication that is visible to the user. Applications generally only 
communicate between sockets of the same class. However, as long as the underlying communication protocol allows it, different types of sockets can still communicate.
There are two different types of sockets 

    The following explanation is more abstract, so don't look at it. 
    Socket is the cornerstone of communication and the basic operation unit of network communication supporting TCP/IP protocol. Sockets can be regarded as 
endpoints for two-way communication between processes between different hosts, which constitute the programming interface within a single host and across the entire network. Sockets 
exist in the communication domain, which is an abstract concept introduced to deal with general thread communication through sockets. Sockets 
usually exchange data with sockets in the same domain (data exchanges may also cross domain boundaries, but in this case some 
kind of interpreter must be executed). Various processes use this same domain to communicate with each other using the Internet Protocol suite.

How sockets work

    To communicate over the Internet, you need at least one pair of sockets, one running on the client side, let's call it 
ClientSocket, and the other running on the server side, let's call it ServerSocket. 
    Depending on how the connection is initiated and the target to which the local socket is to be connected, the connection process between sockets can be divided into three 
steps: server monitoring, client request, and connection confirmation.

    The so-called server monitoring means that the server-side socket does not locate a specific client socket, but is in a state of waiting for a connection to 
monitor the network status in real time. 
    The so-called client request refers to a connection request made by the client's socket, and the target to be connected is the server-side 
socket . To do this, the client's socket must first describe the server's socket to which it wants to connect, point out the 
address and port number of the server-side socket, and then make a connection request to the server-side socket. 
    The so-called connection confirmation means that when the server-side socket monitors or receives the connection request of the client-side socket, 
it responds to the request of the client-side socket, establishes a new thread, and connects the server-side socket to the connection request. The description is sent to the client, and once 
the client confirms this description, the connection is established. The server-side socket continues to be in the listening state and continues to receive 
connection requests from other client-side sockets.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324735180&siteId=291194637