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 the process of calling Socket?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "how the Socket calling process is". Many people will encounter such a dilemma in the operation of actual cases, 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!

The origin of Socket

Before learning a new knowledge, think about why it appears and what problems it solves. Only in this way will I be more impressed.

In the same host, the communication between the two processes is very easy, directly to the operating system to do all kinds of communication details OK. But what if the two processes are on different hosts? How to communicate? And in the actual application scenario, it is very complex, some use TCP protocol, some use UDP protocol, so when we use different protocols to communicate, do we have to use different interfaces? At the same time, we have to deal with various communication details of different protocols, which makes it more difficult to develop and the software is not easy to expand.

One of the programming ideas is "object-oriented". Similarly, when I encounter this problem, can I extract the communication details of different protocols, so that programmers do not need to pay attention to the protocol itself when using it? And this is the origin of Socket.

It provides an interface for process communication between different interconnected hosts. You want to communicate between different hosts? OK, you just call me directly. As for how I implement it, programmers don't need care.

On the basis of the above, we can reach a consensus: if an application needs to communicate between the client and the server, it needs to create a Socket instance. So the question is, how do I use it?

Socket carries out end-to-end communication. I am not clear about how many Lans and routers pass through, so the parameters that can be set are only the network layer and transport layer above the end-to-end protocol. In the network layer, the Socket function needs to specify whether the protocol used is IPv4 or IPv6, which is set to AF_INET and AF_INET6 respectively. In addition, you need to indicate whether you are using TCP or UDP. The TCP protocol is based on data flow, so it is set to SOCK_STREAM, and UDP is based on datagrams, so it is set to SOCK_DGRAM.

Socket calling procedure

After the client and the server create the Socket, the server of the TCP first listens to a port, usually calls the bind function first, and assigns an IP address and port to the Socket. Why do I need an IP address? Sometimes a machine will have multiple network cards, corresponding to multiple IP addresses, you can choose to monitor all network cards, or you can choose to monitor one network card, so that only the packets sent to this network card will be given to you. Then why do I need a port? You know, you are writing an application, and when a network packet comes, the kernel needs to locate your application through this port in the TCP header.

At this point, when the server has the IP and port number, you can call the listen function to listen. In the state diagram of TCP, there is a listen state. When this function is called, the server enters this state, and the client side can initiate the connection.

In the kernel, two queues are maintained for each Socket, one is the queue that has established a connection, which indicates that the three-way handshake has been completed and is in the established state, and the other is the queue that has not fully established the connection, that is to say, the three-way handshake has not been completed and is in the state of syn_rcvd.

Next, the server calls the accept function and takes out a completed connection for processing. If the client has not fully established the connection, there is no other way, just wait.

While the server is waiting, the client can initiate a connection through the connect function. Now the parameter indicates the IP address and port number to connect, and then initiates the three-way handshake. The kernel assigns a temporary port to the client, and once the handshake is successful, the server's accept returns another Socket.

Note that there is a knowledge point that is often examined here, that is, there are two Socket and Socket that are actually used to transmit data, one is called "listening Socket" and the other is called "connected Socket".

After the connection is established, both parties begin to read and write data through read and write functions, just like writing to a file stream. The reason why the Socket of TCP is described as a file stream is that Socket exists as a file in Linux.

In the kernel, Socket is a file, which corresponds to a file descriptor. Each process has a data structure task_struct that points to an array of file descriptors that lists the file descriptors of all files opened by the process. The file descriptor is an integer and is the subscript of the array.

The contents of this array are a pointer to a list of all open files in the kernel. Since it is a file, there will be an inode, but the inode corresponding to Socket is not saved on the hard disk like the real file system, but in memory. In this inode, it points to the Socket structure of Socket in the kernel.

In this structure, there are mainly two queues, one is the sending queue and the other is the receiving queue. What is saved in these two queues is a cache sk_buff. The structure of the complete package can be seen in this cache.

The above is a description of the process of calling functions of Socket programs based on TCP protocol.

Next, let's talk about the process of calling Socket program functions based on UDP protocol.

The procedure of calling Socket program function based on UDP protocol is different from TCP for UDP. First of all, UDP is not connected, so there is no need for a three-way handshake, and there is no need to call listen and connect, but UDP interaction still requires IP and port number, so bind.

UDP does not maintain the connection state, so there is no need to establish a set of Socket for each pair of connections, but as long as there is one Socket to communicate with multiple clients. Also because there is no connection state, sendto and recvfrom are called every time you communicate, so that you can pass in the IP address and port.

This is the end of the content of "how the Socket call process is". 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