gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:33:24 26 4
gpt4 key购买 nike

我正在开发一个嵌入式 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,coming_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}

任何有关问题可能的帮助将不胜感激,因为尝试调试嵌入式设备是相当令人沮丧的。

最佳答案

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

更多详情,请参阅Handling partial return from recv() TCP in C

关于c - recv() 在读取响应数据时中途停止 [C],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23725639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com