不幸的是,在收到浏览器头和收到空缓冲区之间有几秒钟的间隔
recv
现在我可以发送一个响应。
socklen_t clientAddressLength;
int clientSocket = accept(socket, (struct sockaddr *)&socketAddress, &clientAddressLength);
if (clientSocket < 0) {ESP_LOGI(TAG, "failed 3!"); goto network_task_finish;}
if (setsockopt(clientSocket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0)
ESP_LOGI(TAG, "Cannot Set SO_SNDTIMEO for socket");
if (setsockopt(clientSocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0)
ESP_LOGI(TAG, "Cannot Set SO_RCVTIMEO for socket");
while (1) {
ret = recv(clientSocket, recv_buf, NETWORK_RECV_BUF_LEN - 1, 0);
if (ret <= 0) break;
if (ret <= 2 && *recv_buf == '\r') break;
if (ret < NETWORK_RECV_BUF_LEN)
recv_buf[ret] = '\0';
ESP_LOGI(TAG, "RECV");
if (strstr(recv_buf," HTTP/")) {
char *ptr = recv_buf;
if (strncmp(recv_buf, "GET ", 4) == 0)
ptr += 4;
else if (strncmp(recv_buf, "POST ", 4) == 0)
ptr += 5;
else
continue;
ESP_LOGI(TAG, "HEADER!");
}
}
ESP_LOGI(TAG, "RESPONSE");
send(clientSocket, "HTTP/1.1 200 OK\r\n", 17, 0);
send(clientSocket, "Content-Type: text/html\r\n", 25, 0);
send(clientSocket, "Connection: close\r\n", 19, 0);
send(clientSocket, "\r\n", 2, 0);
send(clientSocket, "WORKS", 5, 0);
ESP_LOGI(TAG, "SEND");
日志:
server: RECV
server: HEADER!
... long time ...
server: RESPONSE
server: SEND
处理请求的准确方式是什么?
也许我不必等待它的结束,或者我可以用另一种方式检测标题的结束