gpt4 book ai didi

c++ - 套接字服务器为某些请求返回 tcp 错误 [RST, ACK]

转载 作者:可可西里 更新时间:2023-11-01 02:46:44 29 4
gpt4 key购买 nike

我使用 socket 在 linux 上用 c 开发了一个服务器。

服务器运行正常。如果我从我的网络浏览器向服务器发出请求,服务器会毫无问题地响应请求。

但我在捕获流量(使用 wireshark)时指出,我的服务器有时(很少)返回一个 tcp 错误 [RST,ACK]。

这里在wireshark中显示的tcp错误包之后

TCP error capture

192.168.1.211是我的服务器地址

我检查了“follow tcp stream”,我发现它包含 2 个请求(来自网络浏览器)。我认为它应该只包含给定套接字中的一个请求。我说得对吗?

在跟随 tcp 流之后:

follow tcp stream

我不知道这是否是一个正常错误并且可能发生在 TCP 堆栈中?还是我的代码有问题?

代码:

static void http_cr_new_client(int client, bool service_available)
{
FILE *fp;
char buffer[BUFSIZ];
int8_t auth_status = 0;

fp = fdopen(client, "r+");

while (fgets(buffer, sizeof(buffer), fp)) {
if (!strncasecmp(buffer, "Authorization: Digest ", strlen("Authorization: Digest "))) {
char *username = conf.cpe_userid;
char *password = conf.cpe_passwd;

if (!username || !password) {
// if we dont have username or password configured proceed with connecting to ACS
service_available = false;
goto http_end;
}

if (http_digest_auth_check("GET", "/", buffer + strlen("Authorization: Digest "), REALM, username, password, 300) == MHD_YES)
auth_status = 1;
else
auth_status = 0;
}

if (buffer[0] == '\r' || buffer[0] == '\n') {
/* end of http request (empty line) */
goto http_end;
}
}
if(!service_available) {
goto http_end;
}

http_error:
/* here we are because of an error, e.g. timeout */
goto http_done;

http_end:
if (!service_available) {
MY_LOG (INFO,"Receive Connection Request: Return 503 Service Unavailable");
fputs("HTTP/1.1 503 Service Unavailable\r\n", fp);
fputs("Connection: close\r\n", fp);
} else if (auth_status) {
MY_LOG (INFO,"Receive Connection Request: success authentication");
fputs("HTTP/1.1 200 OK\r\n", fp);
fputs("Content-Length: 0\r\n", fp);
http_success_cr();
} else {
MY_LOG (INFO,"Receive Connection Request: Return 401 Unauthorized");
fputs("HTTP/1.1 401 Unauthorized\r\n", fp);
fputs("Connection: close\r\n", fp);
http_digest_auth_fail_response(fp, "GET", "/", REALM, OPAQUE);
fputs("\r\n", fp);
}
fputs("\r\n", fp);
http_done:
fclose(fp);
close(client);
}

void http_server_init(void)
{
int socket_desc , client_sock , c , *new_sock;
struct sockaddr_in server , client;
static int cr_request = 0;
static time_t restrict_start_time = 0;
time_t current_time;
bool service_available;

for(;;) {
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
MY_LOG (ERROR,"Could not open server socket for Connection Requests");
sleep(1);
continue;
}

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(conf.connection_request_port);

/* enable SO_REUSEADDR */
int reusaddr = 1;
if (setsockopt(socket_desc, SOL_SOCKET, SO_REUSEADDR, &reusaddr, sizeof(int)) < 0) {
MY_LOG (WARNING,"setsockopt(SO_REUSEADDR) failed");
}

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
MY_LOG (ERROR,"Could not bind server socket for Connection Requests");
sleep(1);
continue;
}
break;
}

MY_LOG (INFO,"Connection Request server initiated");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
c = sizeof(struct sockaddr_in);
while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
current_time = time(NULL);
service_available = true;
if ((restrict_start_time==0) ||
((current_time-restrict_start_time) > CONNECTION_REQUEST_RESTRICT_PERIOD))
{
restrict_start_time = current_time;
cr_request = 1;
}
else
{
cr_request++;
if (cr_request > 70)
{
restrict_start_time = current_time;
service_available = false;
}
}
http_cr_new_client(client_sock, service_available);
}

if (client_sock < 0)
{
MY_LOG(ERROR,"Could not accept connections for Connection Requests!");
return;
}
}

注意:我使用 pthread_create()

在主体中启动服务器

最佳答案

看起来您希望在您的代码中只处理一个请求。 HTTP 协议(protocol)在 1.1 版本中添加了持久连接,允许同一个 TCP 套接字被重复用于 1 个以上的请求。

对于持久连接,客户端(您的浏览器)将发送“Connection: keep-alive” header 以表明它支持持久连接(您的浏览器支持)。然后服务器可以用相同的方式回复,或者使用“Connection: close”来表示它不支持持久连接,因此客户端应该关闭当前套接字并为后续请求打开一个新套接字。

在你的例子中,浏览器打开一个套接字连接,发送一个请求并告诉你它支持持久连接。然后您的服务器发送响应并关闭连接。浏览器尝试通过同一连接发送第二个请求但失败,导致出现您看到的错误。

您可以修改您的代码以保持套接字打开并处理多个请求,或者添加“连接:关闭”以告诉客户端您不支持持久连接。

关于c++ - 套接字服务器为某些请求返回 tcp 错误 [RST, ACK],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26125238/

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