C HTTP 服务器/连接重置

发布于 2024-11-08 06:05:56 字数 1154 浏览 0 评论 0原文

我正在尝试用 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;
}

I'm trying to make a tiny http server in c but I got CONNRESET errors with httperf, why ?

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖微凉心微凉 2024-11-15 06:06:16

好的,感谢您的帮助,我刚刚在关闭客户端套接字之前添加了此内容,并且不再出现 CONNRESET 错误:

char readBuffer[128];
char *sendBuffer = "HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Content-Length: 30\r\n\r\n"
    "<html><body>test</body></html>";

do {
    status = recv(accepted, readBuffer, sizeof readBuffer, MSG_DONTWAIT);
} while (status > 0);

send(accepted, sendBuffer, (int) strlen(sendBuffer), 0);

OK, thanks for your help, I've just added this before closing client socket, and no more CONNRESET error :

char readBuffer[128];
char *sendBuffer = "HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Content-Length: 30\r\n\r\n"
    "<html><body>test</body></html>";

do {
    status = recv(accepted, readBuffer, sizeof readBuffer, MSG_DONTWAIT);
} while (status > 0);

send(accepted, sendBuffer, (int) strlen(sendBuffer), 0);
折戟 2024-11-15 06:06:15

一旦您接受它,您就关闭了套接字。因此,连接在另一端重置。

如果您想与 HTTP 客户端对话,则必须解析传入的 HTTP 请求,并使用有效的 HTTP 数据进行回复。 (警告:这不是小事。)

请阅读这篇文章:nweb:例如,一个小型、安全的 Web 服务器(仅限静态页面),它对最小 HTTP 服务器需要完成的工作有一个很好的概述。

You're closing the socket as soon as you accept it. So the connection is reset on the other end of it.

If you want to talk to an HTTP client, you're going to have to parse the incoming HTTP requests, and reply with valid HTTP data. (Warning: that's not trivial.)

Please read this article: nweb: a tiny, safe Web server (static pages only) for example, it has a good rundown of what needs to be done for a minimal HTTP server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文