Home > Software engineering >  About blocking recv returns overtime judgment
About blocking recv returns overtime judgment

Time:09-24

 Windows sockets of VC, the default is blocking recv 
Int nNetTimeout=1000;//1 seconds,
The setsockopt (m_sock, SOL_SOCKET, SO_RCVTIMEO, (char *) & amp; NNetTimeout, sizeof (int));
Int nRet=recv (m_sock...
Printf (" recvlen=% d, errno=% d ", nRet, errno);

Set the socket for the 1 second timeout, you did not receive the message when the 1 second timeout, return to 1, when I want to distinguish between 1 is error or timeout, check online information, said judge errno, but errno print has been on my side for 2, 2 as if said any file or folder does not exist, but this seems to be errors before the code, a judging errno don't seem to be accurate, so

Then I will judge errno to print WSAGetLastError (), can get 10060 WSAETIMEDOUT, but I wonder if I am a multithreaded multiple socket connection, such WSAGetLastError () also seems to be able to accurately judge the current thread is the current thread socket timeout? So feel this way also is not very good,

So could you please tell me how to judge is overtime return 1, or a right of return 1

CodePudding user response:

Recv
The Windows Sockets recv function receives data from a connected socket.

Int recv (
The SOCKET s,
The char FAR * buf,
Int len,
Int flags
);

The Parameters
S
[in] A descriptor identifying A connected socket.
Buf
[out] A buffer for the incoming data.
Len
[in] The length of buf.
Flags
[in] A flag specifying the way in which the call is made.
Few
The recv function is 2 read incoming data on connection - oriented sockets, or connectionless sockets. When using a connection - oriented protocol, The sockets must be connected before calling recv. When using a connectionless protocol, The sockets must be bound before calling recv.

The local address of The socket must be known. The For server applications, use an explicit bind function or an implicit accept or WSAAccept function. The explicit binding is discouraged For The client applications. For client applications The socket can become bound implicitly to a local address using connect, WSAConnect, sendto, WSASendTo, or WSAJoinLeaf.

For connected or connectionless sockets, this function restricts the addresses from which received messages are accepted. The function only returns messages from the remote address specified in the connection. The messages from other addresses are discarded (silently)

For the connection - oriented sockets (For example) with a type SOCK_STREAM, calling recv will return as much information as is currently available - up to the size of the buffer supplied. If the socket has had been configured For the in - line reception of out-of-band data (socket option SO_OOBINLINE) and out-of-band data is yet unread, only out-of-band data will be returned. The application can use the ioctlsocket or WSAIoctl SIOCATMARK command to determine been any more out-of-band data remains to be read.

For connectionless sockets (type SOCK_DGRAM or other message - oriented sockets), the data is extracted from the first enqueued datagram (message) from the destination address specified by the connect function.

If the datagram or message is larger than the buffer supplied, the buffer is filled with the first part of the datagram, and recv generates the error WSAEMSGSIZE. For unreliable separate protocols (For example, UDP) the excess data is lost. For reliable separate protocols, the data is retained by the service provider until it is successfully read by calling recv with a large enough buffer. The for TCP/IP, an application always receive the from any multicast address until after becoming a group member.

If no incoming data is available at the socket, the recv call blocks and waits for data to arrive "according to the blocking rules defined for WSARecv have the MSG_PARTIAL flag not set unless the socket is from. In this case, a value of SOCKET_ERROR is returned with the error code set to WSAEWOULDBLOCK. The select, WSAAsyncSelect, or WSAEventSelect functions provides can be 2 determine the when more data arrives.

If the socket is connection oriented and the remote side has shut down the connection gracefully, and all the data from the had been received, a recv will complete immediately with zero bytes received. If the connection has had been reset, a recv will fail with the error WSAECONNRESET.

The flags parameter can be 2 influence The behaviors of The function invocation beyond The options specified for The associated socket. The semantics of this function are determined by The socket options and The flags parameter. The latter is constructed by The or 'ing The following values:

The Value a fancy
MSG_PEEK Peek at the incoming data. The data is copied into the buffer but is not removed from the input queue. The function then returns the number of bytes currently pending to receive.
MSG_OOB Process out-of-band data. (See section DECnet out-of-band data for a discussion of this topic.)


The Return Values
If no error occurs, recv returns the number of bytes received. If the connection has then gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

The Error Codes
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related