Analysis: setsockopt ()

Source: Internet
Author: User

Analysis: setsockopt ()

1. If
After the socket in the established State (usually distinguished by the port number and the flag) calls closesocket (usually it will not be immediately closed but will go through the time_wait process), you want to continue to reuse the socket:

Bool breuseaddr = true;

Setsockopt (S, sol_socket, so_reuseaddr, (const
Char *) & breuseaddr, sizeof (bool ));

2.
If you want to force close a soket that is already in the connection state after you call closesocket, it does not go through the time_wait process:

Bool bdontlinger = false;

Setsockopt (S, sol_socket, so_dontlinger, (const
Char *) & bdontlinger, sizeof (bool ));

3. In the send () and Recv () processes, sometimes due to network conditions and other reasons, sending and receiving cannot be performed as expected, but set the sending and receiving time limit:

Int nnettimeout = 1000; // 1 second

// Sending time limit

Setsockopt (socket, sol_s0cket, so_sndtimeo, (char
*) & Nnettimeout, sizeof (INT ));

// Receiving time limit

Setsockopt (socket, sol_s0cket, so_rcvtimeo, (char
*) & Nnettimeout, sizeof (INT ));

4. When sending (), the returned bytes are actually sent out (synchronous) or the bytes sent to the socket buffer (asynchronous). The system sends and receives the data once by default.
It is 8688 bytes (about 8.5 KB). In the actual process, the amount of data sent and received is large. You can set the socket buffer to avoid sending (), Recv () continuous Circulation
Ring Transceiver:

// Receiving buffer

Int nrecvbuf = 32*1024; // set it to 32 K

Setsockopt (S, sol_socket, so_rcvbuf, (const
Char *) & nrecvbuf, sizeof (INT ));

// Sending Buffer

Int nsendbuf = 32*1024; // set it to 32 K

Setsockopt (S, sol_socket, so_sndbuf, (const
Char *) & nsendbuf, sizeof (INT ));

5.
If you want to not copy data from the system buffer to the socket buffer during data transmission, the program performance will be affected:

Int nzero = 0;

Setsockopt (socket, sol_s0cket, so_sndbuf, (char
*) & Nzero, sizeof (nzero ));

6. Complete the preceding functions in Recv (). By default, the socket buffer content is copied to the system buffer ):

Int nzero = 0;

Setsockopt (socket, sol_s0cket, so_rcvbuf, (char
*) & Nzero, sizeof (INT ));

7. Generally, when sending a UDP datagram, you want the data sent by the socket to have the broadcast feature:

Bool bbroadcast = true;

Setsockopt (S, sol_socket, so_broadcast, (const
Char *) & bbroadcast, sizeof (bool ));

8. when the client connects to the server, if the socket in non-blocking mode is in the connect () process, you can set the connect () delay until accpet () is called (this function is set

It plays a significant role only in the non-blocking process and does not play a major role in blocking function calls)

Bool bconditionalaccept = true;

Setsockopt (S, sol_socket, so_conditional_accept, (const
Char *) & bconditionalaccept, sizeof (bool ));

9. if closesocket () is called while sending data (sending () is not completed and data is not sent (), in the past, we generally took the "Easy to close" Shutdown (S,

Sd_both), but the data is definitely lost. How can I set the program to meet the requirements of a specific application (that is, to disable the socket after the data is sent out )?

Struct linger {

U_short l_onoff;

U_short l_linger;

};

Linger m_slinger;

M_slinger.l_onoff = 1; // (allowed to stay when closesocket () is called, but there is still data not sent)

// If m_slinger.l_onoff = 0, the function is the same as 2;

M_slinger.l_linger = 5; // (the allowable stay time is 5 seconds)

Setsockopt (S, sol_socket, so_linger, (const
Char *) & m_slinger, sizeof (linger ));

Note: 1. When the latency is set for a non-blocking socket, it is not very useful, it is best not to use; 2. If you want the program to not experience so_linger, you need to set so_dontlinger, or

Set l_onoff = 0;

10. A program that is rarely used in SDI or dialog can record the debugging information of socket:

(I tested this function not long ago, and the mode information can be saved, including the parameters during socket creation.

Specific protocols and error code can be recorded)

Bool bdebug = true;

Setsockopt (S, sol_socket, so_debug, (const
Char *) & bdebug, sizeof (bool ));

11. Additional: the buffer size is usually set through setsockopt (), but it cannot meet the data transmission requirements. I used to write a class for processing network buffering and dynamically allocate memory;

This article is from the csdn blog. For more information, see the source:

Http://blog.csdn.net/ast_224/archive/2008/11/14/3111347.aspx

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.