【发布时间】:2011-05-19 09:07:33
【问题描述】:
我正在尝试在 c 中创建一个小型 http 服务器,但我在使用 httperf 时遇到了 CONNRESET 错误,为什么?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#define SOCKERROR -1
#define SD_RECEIVE 0
#define SD_SEND 1
#define SD_BOTH 2
int server;
int client;
...
int main(int argc, char *argv[])
{
int status;
int accepted;
struct addrinfo hint;
struct addrinfo *info;
struct sockaddr addr;
socklen_t addrsize;
int yes = 1;
...
// client
addrsize = sizeof addr;
while (1)
{
memset(&accepted, 0, sizeof accepted);
memset(&addr, 0, sizeof addr);
accepted = accept(server, &addr, &addrsize);
if (accepted == SOCKERROR) {
warn("Accept", errno);
} else {
shutdown(accepted, SD_SEND);
close(accepted);
}
}
// shutdown
...
return EXIT_SUCCESS;
}
【问题讨论】:
-
httperf 将发送数据,您的应用程序将忽略它...我想要么 perf 工具超时/缓冲区已满并且连接已关闭。如果忽略发送到套接字的 IO,最终一方会放弃,操作系统不会永远缓冲它。
标签: c http connection-reset