Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to use getsockopt and setsockopt in Visual Basic
How to use getsockopt and setsockopt in Visual Basic
1.int Pascal far setsockopt (sockets,int level,int optname,constcharfar * optval,int opt len);

Set options for windows sockets.

S: a descriptive word that identifies a windows socket.

Level: the level defined by the option; Currently only SOL_SOCKET and IPPROTO_TCP layers are supported.

Optname: the option to set.

Optval: Pointer to the buffer where option values are stored.

Optlen: the length of the optval buffer.

The use of setsockopt () is very complicated, and its functions are very rich. The function is used to set the option value of any type and any state of windows socket. Windows sockets has two options: one is Boolean option, which allows or disables a feature; The other is plastic or structural options. If the Boolean option is allowed, optval points to a non-zero integer; Option optval is prohibited from pointing to an integer equal to zero. For Boolean options, optlen should be equal to sizeof (int); For other options, optval refers to an integer or structure containing the desired option, while optlen refers to the length of the integer or structure.

Return value: If no error occurs, setsockopt () returns 0. Otherwise, a SOCKET_ERROR error is returned, and the application can obtain the corresponding error code through WSAGetLastError (). Error code:

WSANOTINITIALISED: Before using this API, you should successfully call WSAStartup ().

WSAENETDOWN:WINDOWS Sockets implementation of WINDOWS detected a network subsystem failure.

WSAEFAULT:optval is not a valid part of the process address space.

WSAEINPROGRESS: A blocked WINDOWS windows socket call is running.

Wsaeinvalid:level value of level is illegal, or the information in optval is illegal.

WSAENETRESET: Connection timed out after setting SO_KEEPALIVE.

WSAENOPROTOOPT: Unknown or unsupported option. The SO_BROADCAST option is not supported by SOCK_STREAM type windows sockets, and the SO_DONTLINGER, SO_KEEPALIVE, SO_LINGER and SO_OOBINLINE options are not supported by SOCK_DGRAM type windows sockets.

WSAENOTCONN: When SO_KEEPALIVE is set, the connection is reset.

WSAENOTSOCK: descriptor is not a windows socket.

Specific uses are as follows:

1.closesocket (usually it doesn't close immediately but goes through the process of TIME_WAIT) wants to continue to reuse the socket:

BOOL bReuseaddr = TRUE

setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)& amp; bReuseaddr,sizeof(BOOL));

2. If you want to forcibly close the connected socket after calling closesocket, without going through the TIME_WAIT process:

BOOL bDontLinger = FALSE

setsockopt(s,SOL_SOCKET,SO_DONTLINGER,(const char *)& amp; bDontLinger,sizeof(BOOL));

3. In the process of send () send (), recv (), sometimes due to network conditions and other reasons, it is impossible to anticipate sending and receiving, and the time limit for sending and receiving is set:

int nNetTimeout = 1000; // 1 sec

//send time limit

setsockopt(socket,SOL _ S0CKET,SO_SNDTIMEO,(char *)& amp; nNetTimeout,sizeof(int));

//receiving time setsockopt(socket, SOL _ S0CKET, SO_RCVTIMEO, (char *)&; nNetTimeout,sizeof(int));

4. When sending (), the bytes actually sent (synchronous) or sent to the socket buffer (asynchronous) are returned; The default transceiver status of the system is 8688 bytes (about 8.5K);

In the actual process, the amount of data sent and received is relatively large, so the socket buffer can be set to avoid the continuous circular sending and receiving of send (), send () and recv ();

//Receive buffer

int nRecvBuf = 32 * 1024;

//set to 32K

setsockopt(s,SOL_SOCKET,SO_RCVBUF,(const char *)& amp; nRecvBuf,sizeof(int));

//send buffer int nsendbuf = 32 *1024; //set to 32K

setsockopt(s,SOL_SOCKET,SO_SNDBUF,(const char *)& amp; nSendBuf,sizeof(int));

5. If you don't want to experience copying from the system buffer to the socket buffer when sending data, it will affect the performance of the program:

int nZero = 0;

setsockopt(socket,SOL _ S0CKET,SO_SNDBUF,(char *)& amp; nZero,sizeof(nZero));

6. Same as in recv () (by default, the contents of the socket buffer are copied to the system buffer):

int nZero = 0;

setsockopt(socket,SOL _ S0CKET,SO_RCVBUF,(char *)& amp; nZero,sizeof(int));

7. Usually, when sending UDP datagrams, it is expected that the data sent by this socket has broadcast characteristics:

BOOL bBroadcast = TRUE

setsockopt(s,SOL_SOCKET,SO_BROADCAST,(const char *)& amp; bBroadcast,sizeof(BOOL));

8. In the process of connecting the client to the server, if the socket in non-blocking mode is in the process of connecting (), you can set the connect () delay until calling accpet () (this function setting only plays a significant role in the non-blocking process, but has little effect in the blocked function call).

BOOL bConditionalAccept = TRUE

setsockopt(s,SOL_SOCKET,SO_CONDITIONAL_ACCEPT,(const char *)& amp; bConditionalAccept,sizeof(BOOL));

9. If closesocket () is called in the process of sending data (send () is not completed, and there is still data not sent), we usually take the measure of "calmly closing" shutdown(s, SD_BOTH) before, but the data is definitely lost. How to set the program to meet the requirements of a specific application (that is, close the socket after sending unfinished data)?

Structural stay

{

u _ short l _ onoff

u _ short l _ linger

};

Linger m _ sLinger

m _ slinger . l _ onoff = 1; //(Stop allowed when closesocket () is called, but there is still data to send) If m _ slinger.l _ onoff = 0; The function is the same as 2. );

m _ slinger . l _ linger = 5; //(Permitted residence time is 5 seconds)

setsockopt(s,SOL_SOCKET,SO_LINGER,(const char *)& amp; m_sLinger,sizeof(linger));

Second, int Pascal far getsockopt (sockets, int level, int optname, charfar * optval, int far * optlen);

S: Descriptive words that identify windows sockets.

Level: The level defined by the option. Only SOL_SOCKET and IPPROTO_TCP are supported.

Windows socket options to get.

Optval: Pointer to the buffer where the obtained option value is stored.

Optlen: a pointer to the optval buffer length value.

Return value: getsockopt () returns 0 if no error occurs. Otherwise, a SOCKET_ERROR error is returned, and the application can obtain the corresponding error code through WSAGetLastError ().

Error code:

WSANOTINITIALISED: Before using this API, you should successfully call WSAStartup ().

WSAENETDOWN:WINDOWS Sockets implementation of WINDOWS detected a network subsystem failure.

Wsaedefault: illegal parameter optlen.

WSAEINPROGRESS: A blocked WINDOWS windows socket call is running.

WSAENOPROTOOPT: Unknown or unsupported option. The SO_BROADCAST option is not supported by SOCK_STREAM type windows sockets, and the SO_ACCEPTCONN, SO_DONTLINGER, SO_KEEPALIVE, SO_LINGER and SO_OOBINLINE options are not supported by SOCK_DGRAM type windows sockets. WSAENOTSOCK: descriptor is not a windows socket.

For example, get the buffer size of recv.

int opt val = 0;

int opt len = sizeof(opt val);

getsockopt(socket,SOL_SOCKET,SO_RCVBUF,(char *)& amp; opt val & amp; opt len);

Temp.format ("buffer size received by socket: optval:% d, optlen:% d", optval, optlen);

AfxMessageBox(temp);