Mongoose C++ HTTP服务器示例给出mg_open_listen失败

xxhby3vn  于 2023-04-30  发布在  Go
关注(0)|答案(1)|浏览(219)

我试图运行Mongoose C++基本HTTP服务器代码(https://mongoose.ws/documentation/tutorials/http-server/),基于他们最新的github master分支-在Windows上的Visual Studio项目中,我得到了mg_open_listen的错误。我不认为问题出在代码上,我只是不明白为什么创建套接字时会出现错误。
下面是控制台:

Starting HTTP listener on http://localhost:8000
271275e 1 mongoose.c:4315:mg_open_liste setsockopt(SO_EXCLUSIVEADDRUSE): 10022
271275e 1 mongoose.c:3346:mg_listen     Failed: http://localhost:8000, errno 0

下面是我的代码:

#include <iostream>
#include "mongoose.h"

static const char* s_listen_on = "http://localhost:8000";
static char s_web_root[512] = ".";

static void eventCallback(struct mg_connection* c, int ev, void* ev_data, void* fn_data)
{
    if (ev == MG_EV_HTTP_MSG)
    {
        struct mg_http_message* hm = (struct mg_http_message*)ev_data;

        if (mg_http_match_uri(hm, "/"))
        {
            mg_http_reply(c, 200, "Content-Type: text/plain\r\n", "Hello, %s\n", "world");
        }

        struct mg_http_serve_opts opts;
        memset(&opts, 0, sizeof(opts));
        opts.root_dir = s_web_root;
        mg_http_serve_dir(c, ev_data, &opts);
    }
    (void)fn_data;
}

int main(int argc, char** argv)
{
    struct mg_mgr mgr;
    mg_mgr_init(&mgr);
    printf("Starting HTTP listener on %s\n", s_listen_on);
    mg_http_listen(&mgr, s_listen_on, eventCallback, NULL);

    for (;;)
    {
        mg_mgr_poll(&mgr, 1000);
    }

    mg_mgr_free(&mgr);
    return 0;
}
agxfikkp

agxfikkp1#

我正在尝试运行Mongoose C++基本HTTP服务器代码,基于他们最新的github master分支。
master分支不稳定,可能有bug。实际上,它有一个bug,正在审查修复:Use SO_EXCLUSIVEADDRUSE on Windows, as intended #2124
如果您不开发Mongoose,请使用最新版本Mongoose 7.9 Latest

相关问题