对于大学项目,我在C中开发了一个HTTP服务器 .
在性能测试的峰值负载期间出现我的问题:在X个同时连接之后,我的服务器不再回答客户端 .
这似乎是因为采用backlog参数的'listen'套接字POSIX函数限制了连接队列 . How could I do to handle X simultaneity connections if the backlog is less than X ?
if (listen(socket_file_descriptor, 10000) < 0) {
perror("Unnable to listen");
exit(EXIT_FAILURE);
}
while (1) {
client_socket_informations_lgth = sizeof(client_socket_informations);
client_file_descriptor_socket =
accept(socket_file_descriptor,
(struct sockaddr *)&client_socket_informations,
&client_socket_informations_lgth);
if (client_file_descriptor_socket < 0) {
perror("Server accept failed.");
exit(EXIT_FAILURE);
} else if (client_file_descriptor_socket > 0) {
pid_t child;
do {
child = fork();
fprintf(stderr, "Fork again");
}
while ((child == -1) && (errno == EAGAIN));
if (child == 0) {
//Handling the client request
} else if (child > 0) {
waitpid(child, NULL, 0);
close(client_file_descriptor_socket);
}
}
}
在我的客户端上,这是我尝试在X线程中连接到我的服务器X次的行,并出现错误:
if (connect(sockfd, (struct sockaddr *) &destAddr, sizeof(struct sockaddr)) == -1) {
fprintf(stderr, "Error with client connecting to server\n");
close(sockfd);
return 0;
}
我想到的更好的解决方案是在接受新连接之前等待同时连接数<到X.但我怎么能这样做?处理问题的另一种方法是告诉客户端重试连接到服务器,直到他收到正确的HTTP答案?但这意味着服务器无法处理峰值负载!我可以想象经典的Web客户端会这样做,但它真的是一个可扩展的解决方案吗?
谢谢