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

Socket buffer and congestion form

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

Share

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

In the "accept and send socket data" section, you can use the write () / send () function to send data and the read () / recv () function to accept data. This section looks at how the data is passed.

Socket buffer

After each socket is created, the city allocates two buffers, an output buffer and an input buffer.

Write () / send () does not immediately transfer data to the collection, but instead writes the data to the buffer, and then the TCP protocol sends the data from the buffer to the destination machine. Once the data is written to the buffer, the functions can go successfully, regardless of whether they have reached the destination mechanically or not, and whenever they are sent to the collection, which is mostly the work of the TCP protocol.

The TCP protocol is independent of the write () / send () function. Data can be sent to the collection as soon as it is written into the buffer, and can also be accumulated in the buffer from time to time. Repeatedly written data is sent to the collection at one time, which depends on the collection status in advance, whether the thread can be free later, and many other factors, which are not controlled by the programmer.

The same is true of the read () / recv () function, which also reads data from the output buffer rather than directly from the collection.

Figure: the TCP socket's Istroke O buffer representation

These Icano buffer features can be rectified as follows:

The iCompo buffer exists independently in each TCP socket

The Imap O buffer is actively generated when the socket is created.

Even closed sockets continue to transmit data left in the input buffer

A closed socket loses the data in the output buffer.

The default size of the output input buffer is usually 8K, which can be obtained by the getsockopt () function:

Unsigned optVal; int optLen = sizeof (int); getsockopt (servSock, SOL_SOCKET, SO_SNDBUF, (char*) & optVal, & optLen); printf ("Buffer length:% d\ n", optVal)

Operational consequences:

Buffer length: 8192

Only examples are given here, which will be explained in detail earlier. Choking form

With regard to TCP sockets (by default), when sending data using write () / send ():

If the length of the free space in the buffer is less than the data to be sent, then write () / send () will be choked (suspend execution) until the data in the buffer is sent to the destination machine, freeing up enough space to wake up the write () / send () function to continue writing data.

2) if the TCP protocol is sending data to the collection, the input buffer will be locked, writing is not allowed, and write () / send () will be choked until the buffer is unlocked at the end of data transmission. Write () / send () will not be woken up.

3) if the data to be written is greater than the maximum length of the buffer, it will be written in batches.

4) Don't go until all data is written to the buffer write () / send ().

When using read () / recv () to read data:

1) reflect on the buffer at first, if there is no data in the buffer, then read it, otherwise the function will be choked until there is no data on the collection.

2) if the length of the data to be read is less than the length of the data in the buffer, then all the data in the buffer cannot be read out at once, and the remaining data will be accumulated from time to time until the read () / recv () function is read again.

3) the read () / recv () function will not go until the data is read, otherwise it will be constantly choked.

This is the choking form of TCP sockets. The so-called congestion means that the previous step has not been completed and the next step will be suspended until the previous step is completed in order to maintain synchronization.

TCP sockets are congested by default and are the most commonly used. Of course, you can also change it to an unchoked form, which we will explain later.

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