WSARecv() 函数使用解析

Posted Jason

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WSARecv() 函数使用解析相关的知识,希望对你有一定的参考价值。

详情参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms741688(v=vs.85).aspx

简述

The WSARecv function receives data from a connected socket or a bound connectionless socket.

The WSARecv function provides some additional features compared with the standard recv function in three important areas:

  • It can be used in conjunction with overlapped sockets to perform overlapped recv operations.
  • It allows multiple receive buffers to be specified making it applicable to the scatter/gather type of I/O.
  • The lpFlags parameter is used both on input and returned on output, allowing applications to sense the output state of the MSG_PARTIAL flag bit. However, the MSG_PARTIAL flag bit is not supported by all protocols.

WSARecv 和标准的recv相比有以下扩展特性:

  1.可以和重叠sockets结合来使用重叠接收操作

  2.允许使用多个接收buffer来应对分散/聚合型IO

  3.lpFlags参数可以用在输入和输出上,允许应用感知输出状态的MSG_PARTIAL 标志位

函数原型

int WSARecv(
  _In_    SOCKET                             s,
  _Inout_ LPWSABUF                           lpBuffers,
  _In_    DWORD                              dwBufferCount,
  _Out_   LPDWORD                            lpNumberOfBytesRecvd,
  _Inout_ LPDWORD                            lpFlags,
  _In_    LPWSAOVERLAPPED                    lpOverlapped,
  _In_    LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);

参数

s: 套接字句柄

lpBuffers: 与Recv函数不同 这里需要一个由WSABUF结构构成的数组

dwBufferCount: 数组中WSABUF结构的数量

lpNumberOfBytesRecvd: 如果接收操作立即完成,这里会返回函数调用所接收到的字节数

lpFlags:A pointer to flags used to modify the behavior of the WSARecv function call. For more information, see the Remarks section.

lpOverlapped:WSAOVERLAPPED structure

lpCompletionRoutine: A pointer to the completion routine called when the receive operation has been completed (ignored for nonoverlapped sockets).

返回值

Error codeMeaning
WSAECONNABORTED

The virtual circuit was terminated due to a time-out or other failure.

WSAECONNRESET

For a stream socket, the virtual circuit was reset by the remote side. The application should close the socket as it is no longer usable. For a UDP datagram socket, this error would indicate that a previous send operation resulted in an ICMP "Port Unreachable" message.

WSAEDISCON

Socket s is message oriented and the virtual circuit was gracefully closed by the remote side.

WSAEFAULT

The lpBuffers parameter is not completely contained in a valid part of the user address space.

WSAEINPROGRESS

A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.

WSAEINTR

The (blocking) call was canceled by the WSACancelBlockingCall function.

WSAEINVAL

The socket has not been bound (for example, with bind).

WSAEMSGSIZE

The message was too large to fit into the specified buffer and (for unreliable protocols only) any trailing portion of the message that did not fit into the buffer has been discarded.

WSAENETDOWN

The network subsystem has failed.

WSAENETRESET

For a connection-oriented socket, this error indicates that the connection has been broken due to keep-alive activity that detected a failure while the operation was in progress. For a datagram socket, this error indicates that the time to live has expired.

WSAENOTCONN

The socket is not connected.

WSAENOTSOCK

The descriptor is not a socket.

WSAEOPNOTSUPP

MSG_OOB was specified, but the socket is not stream-style such as type SOCK_STREAM, OOB data is not supported in the communication domain associated with this socket, or the socket is unidirectional and supports only send operations.

WSAESHUTDOWN

The socket has been shut down; it is not possible to call WSARecv on a socket after shutdown has been invoked with how set to SD_RECEIVE or SD_BOTH.

WSAETIMEDOUT

The connection has been dropped because of a network failure or because the peer system failed to respond.

WSAEWOULDBLOCK

 

Windows NT:  Overlapped sockets: there are too many outstanding overlapped I/O requests. Nonoverlapped sockets: The socket is marked as nonblocking and the receive operation cannot be completed immediately.

WSANOTINITIALISED

A successful WSAStartup call must occur before using this function.

WSA_IO_PENDING

An overlapped operation was successfully initiated and completion will be indicated at a later time.

WSA_OPERATION_ABORTED

The overlapped operation has been canceled due to the closure of the socket.

 

 

以上是关于WSARecv() 函数使用解析的主要内容,如果未能解决你的问题,请参考以下文章

readv()、writev()、WSARecv()、WSASend()

WSASend() 和 WSARecv() 的所有错误代码是不是意味着套接字已断开连接?

使用 WSARecv() 和 IOCP 时如何知道套接字何时收到了 FIN 数据包?

WSARecv 钩子:防止数据包被可执行文件接收

WSARecv 如何使用 lpOverlapped?如何手动发出事件信号?

Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段