Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a socket socket

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is socket socket". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Client-server model

The Internet has penetrated into all aspects of our lives, and its importance is self-evident. When the Internet was first invented in the 1970s, the client-server model was established. This programming model has been in use ever since. We open a website on a computer, chat on Wechat, and open an app is essentially a connection and interaction between the client and the server.

The client-server model operates as follows:

When the client needs a service, such as opening a web page, it sends a request to the server.

After receiving the request, the server will do appropriate processing, such as reading the data needed by the client from the database.

After the server processes the request, it will return the data needed by the client in a certain format, and then send a response to the client.

After receiving the response from the server, the client will process the web page data, and then display it on the display, and then the client will send a request to the server, and so on.

Socket socket

What is the connection between the client and the server? Socket socket, also known as Berkeley socket, was proposed by the University of California, Berkeley in the 1980s. The working mode of socket is shown below:

Server socket preparation:

Initialize socket

Perform a bind binding operation to bind the server's service to an ip address and a specific port

Perform the listen operation to convert the original socket into the server-side socket

Perform the accept operation, block the process on the accept method, and wait for the client to connect

After the client initializes a socket, it can directly call the connet method to connect the socket of the server. After the famous TCP three-way handshake, the client and the server establish a connection and enter the data transmission state.

The client initiates the write write operation, the server receives the data through read, and then write to the client, and the client receives the data with read. When the interaction between the client and the server is completed, the client initiates the close operation and sends a FIN packet to inform the server to close the link.

Socket socket address format

When establishing a connection between the client and the server, you need to know the address of the other party. It's like a courier guy who needs to know which building and which room you live in if he wants to send you a delivery. The ip address and port of the Internet world can accurately find a service on the server, just like which building is more than zero.

When we use socket, we need each other's ip address and port number. The address structure of socket is shown in the program as shown in the following figure.

Sin_family represents the type of address, and the value of IPv4 is the value of AF_INET,IPv6 is AF_INET6. The program can know which type of socket address is through the type of sin_family.

In_port_t represents the port number, and we see that the port number occupies 16 bits, so the maximum number of port numbers is 2 to the 16th power, that is, 65536 ports, that is, the range of zero 65535. Because you need to know each other's port number every time you establish a connection, many well-known services fix their own port number, which is called reserved port. In this way, clients know which port to use to connect to these services, and they don't have to ask every time, for example, port 21 of ftp, port 22 of ssh, port 23 of telnet, port 80 of http.

Universal socket address format

IPv4 socket format address

IPv6 socket address format

Local socket address format

Comparison of several socket address formats

Universal socket address

The address in IPv4 socket format represents the address of IPv4, the address in IPv6 socket format represents the address of IPv6, the local address structure is used for local socket communication, and there is a general address structure. Why should there be a universal address structure?

It turns out that in order to facilitate function calls, methods such as connect,bind,accept need a pointer parameter of the socket address structure:

For convenience, the definition of the function is directly defined by the general address structure pointer, and then the specific address structure pointer is passed in when it is called, is it similar to the polymorphic feature of object-oriented programming?

However, the size of the ipv4 address structure is 16 bytes, the size of the ipv6 address structure is 28 bytes, the maximum size of the local address structure is 110 bytes, and the size of the general address structure is 16 bytes. With the exception of the ipv4 address, all the other addresses are larger than the universal address structure, so what if the other addresses are translated into universal addresses?

Take a closer look. Here, the parameter is the pointer of the address structure, and then there is a parameter of * addrlen, which indicates the length of the address structure, so you only need to take the data of addrlen length, and the specific type can be determined according to sin_family.

This is the end of the content of "what is the socket socket". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report