我正在为一个嵌入式ARM系统的项目工作,在我的HTTP客户端代码上,我遇到了一个奇怪的问题:recv()似乎只读取了大约一半的HTTP服务器和#39 ;回复前的回应......
代码:
#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}
在尝试调试嵌入式设备时,可能会非常感激任何有关问题的帮助是非常令人沮丧的。
答案 0 :(得分:1)
recv()
返回部分数据。要获得完整的响应,您需要在循环中调用recv()
。