You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询Windows Socket recv函数flags参数功能、取值及使用示例

Got it, let's break down each of these recv() flags for you clearly—since you're working with Winsock, I’ll focus on Windows-specific behavior here, which matches your setup. First, a quick recap of the function prototype you referenced:

int recv(
    _In_ SOCKET s,
    _Out_ char *buf,
    _In_ int len,
    _In_ int flags
);
Key recv() Flags Explained

1. MSG_OOB (Out-of-Band Data)

  • Value: 0x0001 (defined directly in winsock2.h)
  • Core Behavior: Tells recv() to read prioritized out-of-band (OOB) data instead of regular in-band bytes. In TCP, OOB data is a single byte sent via send() with the same MSG_OOB flag—this byte skips the normal receive buffer and gets stored separately for urgent access.
  • Notes:
    • You can only read one byte of OOB data per recv() call with this flag.
    • If no OOB data exists, blocking sockets will wait; non-blocking sockets return SOCKET_ERROR with WSAEWOULDBLOCK.
  • Example:
    char oob_byte[2];
    int result = recv(client_socket, oob_byte, 1, MSG_OOB);
    if (result == 1) {
        printf("Received urgent OOB byte: %c\n", oob_byte[0]);
    } else if (WSAGetLastError() == WSAEWOULDBLOCK) {
        printf("No urgent OOB data available right now\n");
    }
    

2. MSG_PEEK

  • Value: 0x0002 (defined in winsock2.h)
  • Core Behavior: Lets you "peek" at incoming data without removing it from the socket's receive buffer. The data stays intact, so your next regular recv() call (without this flag) will read the same bytes again.
  • Use Case: Perfect for checking what data is arriving before committing to consuming it—like figuring out how large a buffer you need for the full message.
  • Example:
    char peek_buf[100];
    int bytes_in_buffer = recv(client_socket, peek_buf, sizeof(peek_buf), MSG_PEEK);
    if (bytes_in_buffer > 0) {
        printf("Peeked %d bytes (not consumed): %.*s\n", bytes_in_buffer, bytes_in_buffer, peek_buf);
        // Now consume the data with a normal recv
        char actual_buf[100];
        int bytes_read = recv(client_socket, actual_buf, sizeof(actual_buf), 0);
        printf("Actually read %d bytes: %.*s\n", bytes_read, bytes_read, actual_buf);
    }
    

3. MSG_WAITALL

  • Value: 0x0008 (this is defined in ws2tcpip.h, not winsock2.h—that’s why you couldn’t find it there!)
  • Core Behavior: Forces recv() to block until it receives all the data you requested (the len parameter), or until an error/connection closure happens.
  • Notes:
    • Only works with blocking sockets; it has no effect on non-blocking sockets (behaves like a regular recv()).
    • If the connection closes before all data arrives, it returns the number of bytes actually received before the closure.
  • Example:
    // We need exactly 50 bytes for our protocol message
    char msg_buf[50];
    int total_received = recv(client_socket, msg_buf, 50, MSG_WAITALL);
    if (total_received == 50) {
        printf("Successfully received full 50-byte message\n");
    } else if (total_received == 0) {
        printf("Connection closed mid-transfer\n");
    } else {
        printf("Partial read or error: got %d bytes\n", total_received);
    }
    
Bonus: Combining Flags

You can mix flags using a bitwise OR (|) for combined behavior. For example, to peek at OOB data (a rare but valid use case):

recv(client_socket, temp_buf, 1, MSG_PEEK | MSG_OOB);

内容的提问来源于stack exchange,提问作者AK_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:14:37