recv() 在读取响应数据时停止 [C]

Posted

技术标签:

【中文标题】recv() 在读取响应数据时停止 [C]【英文标题】:recv() stops halfway through reading response data [C] 【发布时间】:2014-05-18 18:50:51 【问题描述】:

我正在开发一个嵌入式 ARM 系统的项目,在我的 HTTP 客户端代码上我遇到了一个奇怪的问题:recv() 在返回之前似乎只读取了大约一半的 HTTP 服务器响应......

代码:

#define MAX_CONTENT_SIZE 2048

int generate_body(char* method, char* params, int id, char* outbuf) 
    return sprintf(outbuf, "\"method\": \"%s\", \"params\": [%s], \"id\": %d",
        method, params, id);


// Generates header struct at the beginning of program,
// Should only be called ONCE!
void create_headers(getwork_headers *headers) 
    // Note: C99 struct initializers don't seem to work
    // TODO: Investigate why
    headers->post_request = "POST / HTTP/1.1\r\n";
    headers->host_details = "Host: ";
    headers->colon = ":";
    headers->user_agent = "User-Agent: Miner\r\n";
    headers->content_type = "Content-type: application/json\r\n";
    headers->header_auth = "Authorization: Basic ";
    headers->newline = "\r\n";
    headers->mining_ext = "X-Mining-Extensions: MUCHLOGIC\r\n";
    headers->content_length = "Content-Length: ";
    headers->header_end = "\r\n\r\n";


void update_headers(char content[MAX_CONTENT_SIZE], getwork_headers *headers,
    miner_configuration *config, char b64_auth[256], size_t bodysize) 
    memset(content, 0, MAX_CONTENT_SIZE);

    strcat(content, headers->post_request);
    strcat(content, headers->host_details);
    strcat(content, config->host);
    strcat(content, headers->colon);

    // Port in HTTP request must be readable string
    char portbuf[9];
    memset(portbuf, 0, 9);
    snprintf(portbuf, 9, "%d", config->port);

    strcat(content, portbuf);
    strcat(content, headers->newline);
    strcat(content, headers->user_agent);
    strcat(content, headers->content_type);

    // Add the authentication headers
    strcat(content, headers->header_auth);
    strcat(content, b64_auth);
    strcat(content, headers->newline);

    strcat(content, headers->mining_ext);
    strcat(content, headers->content_length);

    // Dynamically change the content length
    char bodybuf[5];
    memset(bodybuf, 0, 5);
    snprintf(bodybuf, 5, "%d", bodysize);
    strcat(content, bodybuf);

    strcat(content, headers->header_end);


int getwork(miner_configuration *config, char auth_str[256]) 
    // Store the HTTP request for later
    char content[MAX_CONTENT_SIZE];

    getwork_headers hdr;
    char body_text[256];
    memset(body_text, 0, 256); // Testing
    generate_body("getwork", "", 0, body_text); // Testing

    create_headers(&hdr);
    update_headers(content, &hdr, config, auth_str, strlen(body_text));

    // Add body to content
    strcat(content, body_text);

    printf(content); // Testing
    sleep(5); // Testing

    // Find the IP address of the server, with gethostbyname
    struct hostent* myhost = gethostbyname(config->host);

    // Create a TCP socket
    int rpc_socket;
    rpc_socket = socket(AF_INET, SOCK_STREAM, 0);

    // Tell the socket to connect to the IP address we found
    struct sockaddr_in sain;
    sain.sin_family = AF_INET;
    sain.sin_port = htons(config->port);
    sain.sin_addr.s_addr = *((unsigned long *)(myhost->h_addr_list[0]));
    connect(rpc_socket, (struct sockaddr *)&sain, sizeof(sain));

    // Send our request
    send(rpc_socket, content, strlen(content), 0);

    int recvd_len;
    char incoming_buffer[MAX_CONTENT_SIZE];

    memset(incoming_buffer, 0, sizeof(incoming_buffer));

    // TODO: Check if recv() is actually sucessful
    recvd_len = recv(rpc_socket, incoming_buffer, (MAX_CONTENT_SIZE - 1), 0);

    if (recvd_len <= 0) 
        return 1; // Error in recv()
    

    incoming_buffer[recvd_len] = 0; // Null-terminate (J.I.C.)
    iprintf(incoming_buffer);

    printf("\nOther side closed connection!\n");

    // Shutdown
    shutdown(rpc_socket, 0);

    closesocket(rpc_socket);
    return 0;

问题似乎出在recv(rpc_socket, incoming_buffer, (MAX_CONTENT_SIZE - 1), 0); 上,因为请求似乎发送得很好

请求:

POST / HTTP/1.1
Host: XXXXXXXX:8332
User-Agent: Miner
Content-type: application/json
Authorization: Basic XXXXXXX
Content-Length: 43

"method": "getwork", "params": [], "id":0

如果尝试调试嵌入式设备相当令人沮丧,我们将不胜感激任何有关问题的帮助。

【问题讨论】:

【参考方案1】:

recv() 允许返回部分数据。要获得完整响应,您需要循环调用recv()

更多详情请见Handling partial return from recv() TCP in C

【讨论】:

以上是关于recv() 在读取响应数据时停止 [C]的主要内容,如果未能解决你的问题,请参考以下文章

写入数据后向套接字发送错误响应

AWS ElasticBeanstalk NodeJS-502错误:从上游读取响应头时,recv()失败(104:对等连接重置)

使用 recv(n) 时,如果 n 大于 MTU,您能保证至少读取整个第 2 层帧吗?

关于 recv 和读取缓冲区 - C Berkeley Sockets

C:你如何通过套接字读取和解包消息?

在socket编程中怎么判断recv是不是接收完成