one, system call and application programming interfacebefore discussing network communication,first identify two concepts:system Calland theapplication Programming Interface (Application programming Interface,api).
The operating system uses the system invocation mechanism to implement control transfer between the application and the operating system .
when an application process initiates a system call, control is passed from the application to the operating system. After an internal procedure is executed by the operating system, the control is returned to the application. for programmers, each system call is very similar to a function call in a general program design, except that the system call passes control to the operating system.
Figure 1, multiple application processes using system invoke mechanism
when an application process initiates a system call, control is passed from the application process to the system call interface . This interface then passes control to the computer's operating system. The operating system forwards this call to an internal procedure and performs the requested operation. once the internal process is executed, control is returned to the application process via the system call interface. The system call Interface is actually an interface for the application process Control and the control of the operating system to convert, that is, the application programming interface API.
from a programming standpoint, theAPI can be seen as an interface between the operating system and the application .
The TCP/IP protocol stack is already residing in the operating system now. There are several system invocation interfaces available for applications that use TCP/IP:
- The Berkeley UNIX operating system defines an API, also known as a Socket interface (socket interface).
- Microsoft has adopted the socket interface API in its operating system, creating a slightly different API, called the Windows socket.
- At/t defines an API for its UNIX system V, abbreviated to TLI (Transport Layer Interface).
in the discussion of network programming, the socket is often used as an interface between the
application process and the
Transport Layer protocol .
sockets have also become part of the operating system kernel.
application process access to the network via sockets
Figure 2, the application process through the socket access to the network
Note: The process above the socket is controlled by the application, and the Transport layer protocol below the socket is controlled by the operating system.
therefore, as long as the application communicates using the TCP/IP protocol, it must interact with the operating system through sockets .
The role of sockets :A socket system call is issued when the application process needs to communicate with the network,
requesting the operating system to create a " socket " for itto
allocate the system resources required for network communication to the application process . The operating system represents the sum of these resources with a number called a
socket descriptor
and returns this number to the application process . This number must be used for network operations performed by the application process. after the communication is complete, the application process notifies the operating system through a system call to close the socket that all resources related to the "number" are recycled.
shows the relationship between the socket descriptor created by the operating system and the socket data structure when the application process issues a socket system call.
Figure 3, calling the socket socket
second, the network programming several commonly used system calls
1, establish the connection stagewhen the socket is created, its port number and IP address are empty, so the application calls bind to indicate the local address (local port number and local IP address) of the socket. when Bind is invoked on the server, the local port number and local IP address are filled in the socket that has been created. The client can not call bind, which is automatically assigned a dynamic port number by the operating system kernel, which is retracted by the system after the communication is over.
after investigating bind, the server must also investigate listen to set the socket to be passive so that it can accept customer's service request at any time. The UDP server does not use listen system calls because it only provides a no-connect service. The server immediately calls accept to extract the connection request from the remote client process. One of the variables that the system calls accept is to indicate the connection from which socket originated.
servers that work in a concurrent manner
Figure 4 Servers that work concurrently
There is always a primary server process and 0 or more subordinate server processes in the server at any time. The primary server process receives the connection request with the original socket, and the connection is established from the server process with the newly created socket and the corresponding client.
Client Case : After the client invokes the socket to create the socket, the client process calls connect to establish a connection to the remote server (this is the active open, which corresponds to the client initiating the connection request). In the Connect system call, the customer must indicate the remote IP address and port number.
2. Data transmission Phaseboth the client and the server use the Send system call to transmit the data and receive the data using the RECV system call. Typically, the customer uses Send to send the request, and the server sends the answer using send. the server uses RECV to receive requests sent by the client with a send call. The customer receives the answer with RECV after the request is made.
calling send requires three parameters: the socket descriptor to which the data is destined, the address of the data to be sent, and the length of the data. calling recv requires three parameters: the socket descriptor to use, the cache address, and the length of the cache space.
3. Connection Release Phaseonce the client or server finishes using the socket, the socket is revoked. Call close to release the connection and undo the socket.
Figure 5, Socket system call Order
Note :
The UDP server provides no connection service and therefore does not use the listen and accept system calls .
Keep in mind the client and server-side invocation order above, and then, for network programming, just focus on the upper logic.
Linux Socket Network Programming detailed