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 functions?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the process of calling Socket functions?". 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!

One: brief introduction

Socket can be used as a socket or slot, can be thought of as a network cable, one end is plugged into the client, the other is plugged into the server, and then communicate. So before communicating, both parties should establish a Socket.

Second: the procedure of calling Socket program function based on TCP protocol

The server of 1.TCP listens to a port first, and usually calls the bind function first to give the Socket an IP address and port.

two。 When the server has the IP and port number, you can call the listen function to listen. In the TCP state diagram, there is a listen state, and when this function is called, the server enters this state

At this point, the client can initiate a connection.

3. In the kernel, two queues are maintained for each Socket. One is the queue that has established the connection, when the three-way handshake of the connection has been completed and is in the established state; the other is that the connection has not been fully established

The queue, which has not completed the three-way handshake at this time, is in the state of syn_rcvd.

4. The server calls the accept function and takes out a completed connection for processing. If it's not finished, you have to wait.

5. While the server is waiting, the client can initiate a connection through the connect function. First indicate the IP address and port number to connect to in the parameters, and then initiate the three-way handshake. The kernel assigns a

Temporary port. Once the handshake is successful, the server's accept returns another Socket.

6. After the connection is successfully established, both parties begin to read and write data through the read and write functions, just like writing to a file stream.

Socket exists in the form of a file in Linux. In addition, there are file descriptors, written and read, also through file descriptors. 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 this process. The file descriptor is an integer, which is the subscript of the array.

Third: the procedure of calling Socket program function based on UDP protocol

1.UDP is not connected, so there is no need for a three-way handshake, so there is no need to call listen and connect, but UDP interaction still requires IP and port number, so bind is also needed.

2.UDP does not maintain connection state, so it is not necessary to establish a set of Socket for each pair of connections, but as long as there is one Socket, it can communicate with multiple clients. Because there is no connection status, I adjust it every time I communicate.

With both sendto and recffrom, you can pass in the IP address and port.

Four: Socket multi-process programming

The number of TCP connections is limited. First of all, the Socket is a file, so the first step is to configure the number of file descriptors through ulimit; another limit is memory.

Multi-process mode is equivalent to setting up a proxy to listen for requests. Once a connection is established, there will be a connected Socket, so you can create a child process and then hand over the interaction based on the connected Socket to the child process to do.

Fifth: Socket multithreading programming

Threads are much lighter than processes, and new threads can also process requests through the connected Socket, thus achieving the purpose of concurrent processing.

Six: IO multiplexing, one thread maintains multiple Socket

There is actually a problem based on the process or threading model. For a new TCP connection, you need to assign a process or thread. C10K, which means that if a machine wants to maintain 10,000 connections, it needs to create 10,000 processes or

Thread, then the operating system cannot bear it.

Because Socket is a file descriptor, all Socket targeted by a thread are placed in a file descriptor FD _ set, and then call the select function to listen for changes in the set of file descriptors.

Each file description. Those file descriptors that change are set to 1 in the corresponding bit of fd_set, indicating that Socket is readable or writable, so that you can read and write, then call select, and then see the next round of changes.

Seven: IO multiplexing, epoll mode

Epoll is implemented in the kernel not by polling, but by registering the callback function to notify when the description of a file changes. This notification method increases the efficiency of monitoring Socket data.

It will not be greatly reduced, and the number of Socket that can be monitored at the same time is also very large. Epoll is called a sharp weapon to solve the C10K problem.

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

Network Security

Wechat

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

12
Report