Analysis: setsockopt () improves program robustness

Source: Internet
Author: User

The article has

Http://hi.baidu.com/chary8088/blog/item/a47bfbd1c01d4fd6562c843f.html

This format is intolerable !!!!

The following format is displayed after I open it in advanced editing mode.

Change to the normal mode, or the following format, messy

Paste the content into notepad, And Then paste it from notepad.

Or

Chaos

I can't stand it. I posted it to the above address.

 

1. if you want to reuse a socket that is already in the established State (usually distinguished by the port number and the flag) after calling closesocket (usually the socket will not be closed immediately but will go through the time_wait process: bool breuseaddr = true; setsockopt (S, sol_socket, so_reuseaddr, (const char *) & breuseaddr, sizeof (bool); 2. if you want to disable soket that is already in the connection state after calling 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 (), Recv () process, sometimes due to network conditions and other reasons, sending and receiving cannot be expected And set the sending and receiving time limit: int nnettimeout = 1000; // 1 second // The sending time limit setsockopt (socket, sol_s0cket, so_sndtimeo, (char *) & nnettimeout, sizeof (INT); // receipt time limit setsockopt (socket, sol_s0cket, so_rcvtimeo, (char *) & nnettimeout, sizeof (INT); 4. when sending (), the returned bytes are actually sent out bytes (synchronized) or the bytes (asynchronous) sent to the socket buffer ); the default sending and receiving status is 8688 bytes (about 8.5 KB). In the actual process, the sending and receiving data volume is large. You can set the socket buffer, instead of sending () and Recv () repeatedly sending and receiving: // The receiving buffer int nrecvbuf = 32*1024; // set it to 32 K setsockopt (s, so Rochelle socket, so_rcvbuf, (const char *) & nrecvbuf, sizeof (INT); // sending buffer int nsendbuf = 32*1024; // set to 32 K setsockopt (S, sol_socket, so_sndbuf, (const char *) & nsendbuf, sizeof (INT); 5. if the program performance is not affected by the copy from the system buffer to the socket buffer when sending data: int nzero = 0; setsockopt (socket, sol_s0cket, so_sndbuf, (char *) & nzero, sizeof (nzero); 6. same as above in Recv () to complete the above function (by default, the content of the socket buffer 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 () called (this function setting only plays a significant role in the non-blocking process and does not play a major role in blocked 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 used to take the "Easy to close" Shutdown (S, sd_both), but the data is definitely lost, how can I set a program to meet the requirements of a specific application (that is, to disable socket after sending unsent data )? Struct linger {u_short l_onoff; u_short l_linger ;}; linger m_slinger; m_slinger.l_onoff = 1; // (this is called in closesocket (), but it is allowed to stay when data is not sent) // If m_slinger.l_onoff = 0; then the function and 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 experience so_linger, you need to set so_dontlinger, or set l_onoff = 0; 10. another rarely used program is SDI or dialog, which can record the debugging information of the socket: (this function has been tested recently, and the debugging information can be saved, including parameters during socket establishment, specific protocols used, 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 am used to writing a class for processing network buffering and dynamically allocating memory; I will write this class below to help beginners:

// Rewrite the string format with the following code: // ================================================ ========================================================== ========/// binary data, it is mainly used to send and receive data in the network buffer. // the source code of the cstring type of the MFC is used as the source code. The usage is similar to that of the cstring. // However, the pure binary data is stored in the ctiobuffer, '/0' is not the end sign of it. // The data length can be obtained through getlength (), and the buffer address can be obtained through the lpbyte operator.

// ================================================ ========================================================== // Copyright (c) all-vision Corporation. all rights reserved. // module: netobject // file: simpleiobuffer. h // Author: gdy119 // Email: 8751webmaster@126.com // Date: 2004.11.26 // ========================================== ========================================================== = // netiobuffer. h # ifndef _ netiobuffer_h # DEFINE _ netiobuffe R_h // ============================================== ========================================================== # define max_buffer_length 1024*1024 // ================================== ========================================================== ========/// main data class for processing network buffer {protected: lpbyte m_pbindata; int m_nlength; int m_ntotallength; critical_sectionm_cs; void initvalibers (); Public E, int nlength); nntiobuffer (const nntiobuffer & binarysrc); Virtual ~ Nntiobuffer (); // ================================================ ====================================================== bool copydata (const lpbyte lbbyte, int nlength); bool concatdata (const lpbyte lbbyte, int nlength); void resetiobuffer (); int getlength () const; bool setlength (INT nlen); lpbyte getcurpos (); int getremainlen (); bool isempty () const; operator lpbyte () const; static getmaxlength () {return max_buffer_length;} const accounts buffer & operator = (const accounts buffer & buffsrc );}; # endif // netobuffer. CPP: Implementation of the authorized buffer class. // ================================================ ====================================#include "stdafx. H "# include" netiobuffer. H "// ============================================ ============================================= ========================================================== ========================================/// construction/destruction nntiobuffer:: Maid () {initvalibers ();

} Nntibuffer: nbbyte (const lpbyte lbbyte, int nlength) {initvalibers (); copydata (lbbyte, nlength);} ntiobuffer ::~ Nntiobuffer () {Delete [] m_pbindata; m_pbindata = NULL; deletecriticalsection (& m_cs );

} Nntibuffer: ncitibuffer (const ncitibuffer & binarysrc ){

Initvalibers (); copydata (binarysrc, binarysrc. getlength ());

} Void MAID: initvalibers (){

M_pbindata = NULL; m_nlength = 0; m_ntotallength = max_buffer_length; If (m_pbindata = NULL) {m_pbindata = new byte [m_ntotallength]; Assert (m_pbindata! = NULL);} reverse (& m_cs);} void: resetiobuffer () {entercriticalsection (& m_cs); m_nlength = 0; memset (m_pbindata, 0, m_ntotallength ); leavecriticalsection (& m_cs );}

Bool nntiobuffer: copydata (const lpbyte lbbyte, int nlength) {If (nlength> max_buffer_length) return false;

Resetiobuffer (); entercriticalsection (& m_cs); memcpy (m_pbindata, lbbyte, nlength); m_nlength = nlength; leavecriticalsection (& m_cs );

Return true ;}

Bool nntiobuffer: concatdata (const lpbyte lbbyte, int nlength) {If (m_nlength + nlength> max_buffer_length) return false;

Entercriticalsection (& m_cs); memcpy (m_pbindata + m_nlength, lbbyte, nlength); m_nlength + = nlength; leavecriticalsection (& m_cs );

Return true ;}

Int x0tiobuffer: getlength () const {return m_nlength ;}

Bool nntiobuffer: setlength (INT nlen) {If (nlen> max_buffer_length) return false;

Entercriticalsection (& m_cs); m_nlength = nlen; leavecriticalsection (& m_cs );

Return true ;}

Lpbyte MAID: getcurpos (){

If (m_nlength <max_buffer_length)

Return (m_pbindata + m_nlength );

Else return NULL ;}

: Operator lpbyte () const {return m_pbindata ;}

Int MAID: getremainlen (){

Return max_buffer_length-m_nlength;

} Bool nntiobuffer: isempty () const {return m_nlength = 0 ;}

Const MAID: Operator = (const maid & buffsrc) {If (& buffsrc! = This) {copydata (buffsrc, buffsrc. getlength ());

} Return * this;

}

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.