WinSock:从套接字中删除数据
WinSock: Remove data from socket
我想在不指定缓冲区的情况下从套接字接收数据。所以我只想从传入数据缓冲区中删除 x 个字节。
我有以下代码(截断):
recv(gSocket, NULL, userDataLength, 0);
但是当我执行上面的代码时,recv()
的 return 值是 SOCKET_ERROR
和 WSAGetLastError()
returns WSAECONNABORTED
。此外,我的连接已关闭。
问题:使用接收函数从套接字的RX缓冲区中删除数据是不可能的吗?
I want to receive data from a socket without specifing a buffer.
抱歉,那根本不可能。您必须提供一个缓冲区来接收字节。如果您不想使用字节,只需在读入缓冲区后丢弃它即可。
I just want to remove x bytes from the incoming data buffer.
没有 API。您必须将字节读入有效缓冲区。之后如何处理该缓冲区由您决定。
when I execute the code above the return value of recv()
is SOCKET_ERROR
and WSAGetLastError()
returns WSAECONNABORTED
.
我本以为 WSAEFAULT
而不是。
Is it not possible, to use the receive function for removing data from the RX buffer of the socket?
当然,这是可能的。您只需要将字节读入缓冲区,即使它只是一个临时缓冲区,例如:
int ignoreBytes(SOCKET sock, int numBytes)
{
u_char buf[256];
while (numBytes > 0)
{
int numRead = recv(sock, (char*)buf, min(sizeof(buf), numBytes), 0);
if (numRead <= 0)
return numRead;
numBytes -= numRead;
}
return 1;
}
ignoreBytes(gSocket, userDataLength);
我想在不指定缓冲区的情况下从套接字接收数据。所以我只想从传入数据缓冲区中删除 x 个字节。
我有以下代码(截断):
recv(gSocket, NULL, userDataLength, 0);
但是当我执行上面的代码时,recv()
的 return 值是 SOCKET_ERROR
和 WSAGetLastError()
returns WSAECONNABORTED
。此外,我的连接已关闭。
问题:使用接收函数从套接字的RX缓冲区中删除数据是不可能的吗?
I want to receive data from a socket without specifing a buffer.
抱歉,那根本不可能。您必须提供一个缓冲区来接收字节。如果您不想使用字节,只需在读入缓冲区后丢弃它即可。
I just want to remove x bytes from the incoming data buffer.
没有 API。您必须将字节读入有效缓冲区。之后如何处理该缓冲区由您决定。
when I execute the code above the return value of
recv()
isSOCKET_ERROR
andWSAGetLastError()
returnsWSAECONNABORTED
.
我本以为 WSAEFAULT
而不是。
Is it not possible, to use the receive function for removing data from the RX buffer of the socket?
当然,这是可能的。您只需要将字节读入缓冲区,即使它只是一个临时缓冲区,例如:
int ignoreBytes(SOCKET sock, int numBytes)
{
u_char buf[256];
while (numBytes > 0)
{
int numRead = recv(sock, (char*)buf, min(sizeof(buf), numBytes), 0);
if (numRead <= 0)
return numRead;
numBytes -= numRead;
}
return 1;
}
ignoreBytes(gSocket, userDataLength);