mongoose web服务器helloworld程序 [英] mongoose web server helloworld program

查看:537
本文介绍了mongoose web服务器helloworld程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个名为mongoose的嵌入式网络服务器,并 http://code.google.com/p/mongoose/和我读的wiki它是伟大的,我搜索一些样本hello世界程序,但我找不到它...我发现了一些例子,但是用c ++编写的windows和任何一个提供一个示例c程序

I came across an embedded web server named mongoose and http://code.google.com/p/mongoose/ and I read the wiki it was great and i searched for some sample hello world program but i couldn't find it... i found some example but that was written in c++ for windows and can any one provide an example c program to run this webserver..

推荐答案

很简单,首先你需要实现回调函数:

It is quite simple, first you need to implement the call back function:

void *event_handler(enum mg_event event,
    struct mg_connection *conn) {

    const struct mg_request_info *request_info = mg_get_request_info(conn);

    static void* done = "done";

    if (event == MG_NEW_REQUEST) {
        if (strcmp(request_info->uri, "/hello") == 0) {
            // handle c[renderer] request
            if(strcmp(request_info->request_method, "GET") != 0) {
                // send error (we only care about HTTP GET)
                mg_printf(conn, "HTTP/1.1 %d Error (%s)\r\n\r\n%s",
                    500,
                    "we only care about HTTP GET",
                    "we only care about HTTP GET");
                // return not null means we handled the request
                return done;
            }

            // handle your GET request to /hello
            char* content = "Hello World!";
            char* mimeType = "text/plain";
            int contentLength = strlen(content);

            mg_printf(conn,
                "HTTP/1.1 200 OK\r\n"
                "Cache: no-cache\r\n"
                "Content-Type: %s\r\n"
                "Content-Length: %d\r\n"
                "\r\n",
                mimeType,
                contentLength);
            mg_write(conn, content, contentLength);
            return done;
            }
        }
        // in this example i only handle /hello
        mg_printf(conn, "HTTP/1.1 %d Error (%s)\r\n\r\n%s",
            500, /* This the error code you want to send back*/
            "Invalid Request.",
            "Invalid Request.");
        return done;
    }

    // No suitable handler found, mark as not processed. Mongoose will
    // try to serve the request.
    return NULL;
}

然后您需要启动服务器:

Then you need to start the server:

int main(int argc, char **argv) {

    /* Default options for the HTTP server */
    const char *options[] = {
        "listening_ports", "8081",
        "num_threads", "10",
        NULL
    };

    /* Initialize HTTP layer */
    static struct mg_context *ctx;

    ctx = mg_start(&event_handler, options);
    if(ctx == NULL) {
        exit(EXIT_FAILURE);
    }

    puts("Server running, press enter to exit\n");
    getchar();
    mg_stop(ctx);

    return EXIT_SUCCESS;
}

这篇关于mongoose web服务器helloworld程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆