Clarify the concept of granting an address to a socket
Source: Internet
Author: User
Article Title: clarify the concept of granting an address to a socket. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
First
Over time, I will constantly add something to this article, knowing that it should be over.
This document is actually part of my learning summary. If someone can send me a letter to point out the mistakes or deficiencies, I am grateful.
I assume that the reader is familiar with the socket connection establishment process and various State Transformations, because the purpose of this document is to clarify the concept, not to introduce the concept.
When using socket programming, we all know that a connection must be established before network communication, and the connection is established through some operations on the socket. The connection process can be roughly divided into the following steps:
1. Establish a socket.
2. assign an address to a socket. This address is not a common concept of a network address.
3. Establish a socket connection.
1. Establish socket
When using socket to establish a socket, we actually establish a data structure. The most important information about this data structure is specifying the connection type and protocol used. In addition, there are some structure fields about the connection queue operation (they are not involved here ).
After we use the socket function, if it succeeds, an int-type descriptor will be returned, pointing to the previous socket Data Structure maintained in the kernel. All our operations work on that data structure through this descriptor. This is like getting a file descriptor after creating a file. operations on the file are carried out through the file descriptor, rather than directly acting on the inode data structure. I used the file descriptor as an example because the socket data structure is also closely related to the inode data structure. It does not exist independently in the kernel, but is located in a VFS inode structure. Therefore, there are some abstract features that we can use file operations to make an analogy improperly to deepen our understanding.
As mentioned above, after this socket is established, we can obtain a socket descriptor like a file descriptor. As we operate on files, we can write data to the socket to transfer the data to the specified place, which can be a remote host, it can also be a local host. If you are interested, you can also use the socket mechanism to implement IPC, but the efficiency is relatively low. Just try it (I have not tried it ).
2. Assign the address to the socket
Depending on the purpose of establishing a socket, there are two ways to assign the socket address: bind for the server and connetc for the client.
Bind:
As we all know, as long as an IP address is used, prot can distinguish a TCP/IP connection (of course, this connection refers to a connection channel. If you want to distinguish the connection between a specific host, also requires the third property hostname ).
We can use the bind function to assign the communication address and port to a socket used in the server routine.
Here we call the communication IP address and port together constitute a socket address, and the process of specifying a socket to use a specific IP address and port combination for traffic is to assign this socket an address.
To assign a socket address, you must use a Data Structure to specify a specific socket address, which is struct sockaddr. I will not talk about its use, because the purpose of this document is to clarify the concept rather than describe how to use it. The function of the Bind function is to associate the data structure labeled with the socket address with the socket, that is, to assign the socket an address. But I still don't know how the two of them are associated with each other.
The life cycle of a specific socket address is before the connection is closed after the bind is successful. You can create a socket data structure and a socket address data structure. However, before there is no bind, the two have no relationship. After bind, the two have a relationship. This relationship is maintained until the end of the connection. When a connection ends, both the socket data structure and the socket address data structure still exist, but there is no relationship between the two. If you use this socket to reconnect to the socket address, you need to bind the two of them again. Again, I said this connection is a connection channel, rather than a connection between a specific host.
The IP address specified by Bind is usually a local IP address (not specifically specified, but declared by INADDR_ANY), and the main function is to specify the port. After the socket on the server is bind, listen is used to prepare the connection on this socket address.
Connect:
For the client, bind is not used (not unusable, but meaningless), and they will establish the relationship between the socket and the socket address through the connet function. The socket address is the socket address of the server to be connected. While establishing the relationship between the socket and the socket address, connect is also trying to establish a remote connection.
3. Establish a socket connection
To establish a connection, the server requires two steps: bind, listen; and the client requires one step: connct. If the server accept is a connect, and the client obtains the accept confirmation, a connection is established.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service