Network Programming FAQ Collection

Source: Internet
Author: User
Tags socket

FAQ for Network programming Management collection

Summary of common problems in network programming

Some of the problems encountered in the network program are summarized, here is mainly for our commonly used TCP sockets related to the summary, there may be errors, there are any questions welcome to ask.

For more detailed instructions on network programming refer to the following books

"UNIX Network Programming" "TCP/IP detailed" UNIX Environment advanced programming

Non-blocking IO and blocking IO:

In network programming, the concept of blocking IO and non-blocking IO is encountered for a network handle, and here is a description of the two sockets first

Basic concept: The blocking mode of the socket means that IO operations, including errors, must be completed before they are returned. Non-blocking mode returns immediately, regardless of whether the operation completes, and requires other ways to determine whether the operation is successful or not.

Set up:

Generally, there are two ways to Fcntl settings and Recv,send series parameters for a socket that is blocking or non-blocking mode.

The FCNTL function can set a socket handle to a non-blocking mode:

Flags = FCNTL (SOCKFD, F_GETFL, 0); Fcntl (SOCKFD, F_SETFL, Flags | O_nonblock); Every action for SOCKFD after Setup is non-blocking.

Recv, the end of the Send function has a flag parameter that can be set to msg_dontwait temporarily set SOCKFD to non-blocking mode, regardless of whether it is blocking or non-blocking. recv (SOCKFD, Buff, buff_size, msg_dontwait); Send (SCOKFD, buff, buff_size, msg_dontwait);

Difference:

Read:

The essence of reading can not be read, in practice, the specific received data is not carried out by these calls, is due to the system at the bottom of the automatic completion, read or RECV is responsible for the data from the bottom buffer copy to our designated location. For reading (read, or recv), in the case of congestion, if the data is not found in the network buffer will be waiting, when the data are found to read to the user specified buffer, but if this time to read a small amount of data, than the specified length of the parameter is smaller, Read does not wait any longer, but returns immediately. The principle of read is how much data is read at no more than the specified length, and waits for no data. So in general, we read the data need to read the data in a circular read, a read completed can not guarantee that we need to read the length of the data, read the end of the need to determine the length of the data read to decide whether it needs to read again. In the case of non-blocking, the behavior of read is to return directly if no data is found, and if there is data, then how much read is used to process. For reading, the difference between blocking and non-blocking is whether the data is returned immediately when it arrives.

Recv has a msg_waitall parameter recv (SOCKFD, Buff, buff_size, msg_waitall), under normal circumstances recv will wait until read to the buff_size length of the data, But here the WaitAll is also just try to read the whole, in the case of interruption recv or may be interrupted, causing not read the specified length of the buff_size. Therefore, even if the use of recv + WaitAll parameters or to consider whether the need to cycle read the problem, in the experiment for most cases recv can still read the buff_size, so the corresponding performance will be more than direct read for the cycle of reading better. However, note that this time the SOCKFD must be in blocking mode, otherwise WaitAll will not work.

Write:

Writing is not the nature of the send operation, but the user state of the data copy to the bottom of the system, and then the system to send operations, the return of success only means that the data has been copy to the underlying buffer, and does not represent data and issued, but also can not represent the end has received data.

In the case of write (or send), the blocking situation is always waiting until the data in the Write complete section returns. This behavior is different from the reading operation, the main reason is that when we read the data we do not know the end of the data, when the data is sent at the end, if the wait can cause a dead loop, so there is no deal in this regard, and for write, due to the need to write the length is known, So you can write it all the time until you finish it. However, the problem is that write may be interrupted to write only one part of the data at a time, so the write process still needs to consider cyclic write, but most of the time the write call may succeed.

In the case of non-blocking writing, it is a strategy to write as many as you can. What's different from reading is how much of the reading is transmitted from the end of the network to the standard, however, for how much can be written by the local network congestion as the standard, when the network congestion is serious, the network layer does not have enough memory to write operations, this time there will be unsuccessful writing, Blocking case will be as far as possible (possibly interrupted) wait until the data is all sent out, for non-blocking situation is a write how much, without interruption in the case also will appear to a part of the situation.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.