Windows network programming experience summary

xiaoxiao2021-03-06  39

1. If the socket (generally distinguished by the port number and the marker) is called in the Established state

CloseSocket (generally not immediately shut down the process of experiencing the time_wait), then 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 are already in the connection status, the SOKET is forced to turn off after calling CloseSocket.

Time_wait process:

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 (), returns the byte (synchronization) actually sent or sent to the Socket buffer.

(Asynchronous); system default status transmission and reception of 8688 bytes (approximately 8.5k); send data in the actual process

And the amount of received data is relatively large, you can set the Socket buffer, and avoid send (), RECV () constant loop transceiver:

// 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 the data is sent, it is expected that it will not experience the copy of the system buffer to the Socket buffer.

Program performance:

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 is in the process () process

To set up the connect () delay until the ACCPET () is called (this function setting is only a significant role in the process of non-blocking, there is no big action in the bundled function.)

Bool bconditionaCcept = 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 ().

Generally taken measures "calmly shut down" ShutDown (S, SD_BOTH), but data is definitely lost, how to set up the program to meet specific

The application requirements (ie, let the data that have not been sent out after turning 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. In the setup delay, it is best not to use for a non-blocking socket.

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:

CODE:

/ / 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, and '/ 0' does not have its end flag. // 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: [email] 8751Webmaster@126.com [/ email]

// 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;

}

[Ctrl a SELECT ALL]

转载请注明原文地址:https://www.9cbs.com/read-55611.html

New Post(0)