Windows network programming experience summary
Transfer from: 9CBS netizens, its ID: GDY119 (night wind cool)
1. If you have called ClosSocket (generally not immediately shut down) in the eStabLished state (generally not immediately shut down), you want to continue to reuse the socket:
Bool BreuseAddr = True;
Setsockopt (S, SOL_Socket, SO_REUSEADDR, (Const Char *) & BreuseAddr, SizeOf (BOOL));
2. If the SOKET to be in the connection status is forced to turn off after calling CloseSocket, there is no process of time_wait:
Bool bdontlinger = false;
Setsockopt (S, SOL_Socket, SO_DONTLINGER, (const char *) & bdontlinger, sizeof (bool));
3. In the seund (), the Recv (), sometimes due to network conditions, etc., the revenue cannot be expected, and set the transmission and reception time limit:
INT nnettimeout = 1000; // 1 second
// Send a time limit
Setsockopt (Socket, SOL_S0CKET, SO_SNDTIMEO, (CHAR *) & nnetTimeout, SizeOf (int));
// Receive time limit
Setsockopt (Socket, SOL_S0CKET, SO_RCVTIMEO, (CHAR *) & nnetTimeout, SizeOf (int));
4. When send (), the return is the byte (synchronization) actually sent or sent to the byte (asynchronous) of the Socket buffer; the default state of the system is transmitted and received once a 8688 bytes (about 8.5) K); In the actual process, the data and the received amount of received data are relatively large, and the Socket buffer can be set, and send (), RECV () continuous loop proceed:
// Receive buffer
INT NRECVBUF = 32 * 1024; // Set to 32K
Setsockopt (S, SOL_Socket, SO_RCVBUF, (const char *) & nrecvbuf, sizeof (int));
// Send buffer
INT NSENDBUF = 32 * 1024; // Set to 32K
Setsockopt (S, SOL_Socket, SO_SNDBUF, (const char *) & nsendbuf, sizeof (int));
5. If it is sent to the data, it is desirable to affect the performance of the program from the system buffer to the Socket buffer:
INT NZERO = 0;
Setsockopt (socket, SOL_S0CKET, SO_SNDBUF, (Char *) & nzero, SizeOf (Nzero));
6. Complete the above function in RECV () (by default, copy the contents of the Socket buffer to the system buffer):
INT NZERO = 0;
Setsockopt (Socket, SOL_S0CKET, SO_RCVBUF, (Char *) & nzero, sizeof (int));
7. Generally, when sending a UDP datagram, it is desirable that the data sent by the socket has broadcast characteristics:
Bool bbroadcast = true;
Setsockopt (S, SOL_Socket, SO_Broadcast, (const char *) & bbroadcast, sizeof (bool));
8. During the Client Connection Server, if the socket in non-blocking mode can set the connection () delay in the process () process until the ACCPET () is called (this function setting is only in the process of non-blocking) Significant role, there is no big effect in the blocking function call) BOOL BCONDITONALACCEPT = TRUE
Setsockopt (S, SOL_Socket, SO_CONDITIONAL_ACCEPT, (Const Char *) & bconditionaCcept, SizeOf (BOOL));
9. If there is no completion of the data (Send () is not completed, there is no transmission), and we call usSocket ().
The general taken measures are "calmly shut down" ShutDown (S, SD_BOTH), but the data is definitely lost, how to set up the requirements of the specific application (ie, let the data that have not finished, turn off the Socket)?
Struct linger {
U_SHORT L_ONOFF;
U_SHORT L_LINGER;
}
linger m_slinger;
m_slinger.l_onoff = 1; // ((in closesocket () call, but there is no stay when the data is not sent, allowed to stay)
// If m_slinger.l_onoff = 0; the function and 2.) the same effect;
m_slinger.l_linger = 5; // (allowed to stay for 5 seconds)
Setsockopt (S, SOL_Socket, SO_Linger, (const char *) & m_slinger, sizeof (linger));
Note: 1. When the stay is set, it is best to do with a non-blocking socket. It is best not to use;
2. If you want a program that does not experience SO_Linger, you need to set SO_DONTLINGER, or set l_onoff = 0;
10. Also, less useful is in the SDI or Dialog program, you can record the status of the Socket:
(The test can be done soon, the adjustment information can be saved, including the parameters of the Socket Establishment, adopted
Specific agreements, as well as the wrong code can be recorded)
Bool BDebug = true;
Setsockopt (S, SOL_Socket, SO_Debug, (const char *) & bdebug, sizeof (bool));
11. Additional: The buffer size is often set via setsockopt (), but it is not possible to meet the data transmission needs,
My habit is to write a class that handles network buffers, dynamically allocate memory; I will write this class, I hope to
Beginners help:
/ / Follow-up String
/ / =========================================================================================================================================================================================== =====================
// binary data, mainly used to transmit data from network buffers
// CNetiobuffer is rewritten as the source code of MFC class CString, and the usage is similar to cstring, // But the pure binary data is stored in CNetioBuffer, '/ 0' does not have its end sign.
// The length of its data can be obtained by getLength (), the buffer address can be obtained by operator LPBYTE.
/ / =========================================================================================================================================================================================== =====================
// 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 _netiobuffer_h
/ / =========================================================================================================================================================================================== =====================
#define max_buffer_ley 1024 * 1024
/ / =========================================================================================================================================================================================== ====================== // Mainly used to handle network buffer data
Class CNetiobuffer
{
protected:
LPBYTE M_PBINDATA;
INT M_NLENGTH;
INT m_NTOTALLENGTH;
Critical_sectionm_cs;
VoidiniBERIBERS ();
PUBLIC:
CNETIOBUFFER ();
CNETIOBUFFER (Const lpbyte lbyte, int nlength);
CNetiobuffer (Const CNetiobuffer & BinarySRC);
Virtual ~ CNetiobuffer ();
/ / =========================================================================================================================================================================================== =====================
Bool CopyData (Const lpbyte lbbyte, int NLENGTH);
Bool Concatdata (Const lpbyte lbbyte, int NLENGTH);
Void resetiobuffer ();
INT getLength () const;
Bool setLength;
LPBYTE getCurpos ();
Int getRemainlen ();
Bool isempty () const;
Operator lpbyte () const;
Static getMaxLength () {return max_buffer_ley
Const CNetiobuffer & Operator = (Const CNetiobuffer & Buffsr);
}
#endif //
// Netobuffer.cpp: Implementation of the CNetiobuffer Class.
/ / =========================================================================================================================================================================================== ===================== # include "stdafx.h"
#include "netiobuffer.h"
/ / =========================================================================================================================================================================================== =====================
/ / =========================================================================================================================================================================================== =====================
// construction / destruction
CNETIOBUFFER :: CNetiobuffer ()
{
INITVALIBERS ();
}
CNETIOBUFFER :: CNetiobuffer (Const lpbyte lbyte, int nlength)
{
INITVALIBERS ();
CopyData (lbbyte, nlength);
}
CNETIOBUFFER :: ~ CNetiobuffer ()
{
delete [] m_pbindata;
M_PBINDATA = NULL;
DeletecriticalSection (& M_CS);
}
CNETIOBUFFER :: CNetiobuffer (Const CNetiobuffer & BinarySRC)
{
INITVALIBERS ();
CopyData (binarysrc, binarysrc.getlength ());
}
Void CNetiobuffer :: 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);
}
InitializeCriticalSection (& M_CS);
}
Void CNetiobuffer :: Resetiobuffer ()
{
ENTERCRITICALSECTION (& M_CS);
m_nlength = 0;
MEMSET (M_PBINDATA, 0, M_NTOTALLENGTH);
LeavecriticalSection; & M_CS);
}
BOOL CNETIOBUFFER :: CopyData (const lpbyte lbyte, 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 CNetiobuffer :: Concatdata (Const lpbyte lbyte, 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 CNETIOBUFFER :: getLength () const
{
Return m_nlength;
}
Bool CNetiobuffer :: SETLENGTH (Int Nlen)
{
IF (Nlen> Max_Buffer_Length)
Return False;
ENTERCRITICALSECTION (& M_CS);
m_nLength = Nlen;
LeavecriticalSection; & M_CS);
Return True;
}
LPBYTE CNETIOBUFFER :: getCurpos ()
{
IF (m_nlength Return (M_PBINDATA M_NLENGTH); Else Return NULL; } CNETIOBUFFER :: Operator lpbyte () const { Return M_PBindata; } Int CNetiobuffer :: getRemainlen () { RETURN MAX_BUFFER_LENGTH - M_NLENGTH; } Bool CNETIOBUFFER :: ISempty () consty { Return m_nlength == 0; } Const CNetiobuffer & CNetiobuffer :: Operator = (Const CNetiobuffer & Buff) { IF (& buffsrc! = this) { CopyData (buffsrc, buffsrc.getlength ()); } RETURN * THIS; }