我遇到了一個名爲mongoose和http://code.google.com/p/mongoose/的嵌入式web服務器,我讀了這個wiki很棒,我搜索了一些示例hello world程序,但是我找不到它...我發現了一些例子但是,這是用C++編寫的Windows和任何一個可以提供一個例子C程序來運行這個網絡服務器..貓鼬網絡服務器helloworld程序
18
A
回答
23
這是很簡單,首先你需要實現回調函數:
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;
}
然後您需要啓動服務器:
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;
}
4
我寫了一個使用Mongoose的C++ REST服務庫。這裏有一個簡單的例子:
#include <iostream>
#include <server/server.hpp>
int main()
{
using namespace pwned::server;
Server server;
server.Get("/", [](mg_event*, Params const &) {
return Server::response("Hello!");
});
std::cin.get();
}
基於 https://github.com/nurettin/pwned/blob/master/examples/server/basics/server.cpp
0
編譯應用程序://代碼:$ CC my_app.c mongoose.c
#include "mongoose.h" // Include Mongoose API definitions
static const char *s_http_port = "8089";
// Define an event handler function
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data)
{
struct mbuf *io = &nc->recv_mbuf;
switch (ev)
{
case MG_EV_RECV:
// This event handler implements simple TCP echo server
mg_send(nc, io->buf, io->len); // Echo received data back
mbuf_remove(io, io->len); // Discard data from recv buffer
break;
default:
break;
}
}
int main(void)
{
struct mg_mgr mgr;
mg_mgr_init(&mgr, NULL); // Initialize event manager object
// Note that many connections can be added to a single event manager
// Connections can be created at any point, e.g. in event handler function
mg_bind(&mgr, s_http_port, ev_handler); // Create listening connection and add it to the event manager
for (;;)
{
// Start infinite event loop
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}
相關問題
- 1. 貓鼬網絡服務器是否有關閉處理程序?
- 2. 貓鼬網絡服務器和IE9瀏覽器
- 3. 如何在Linux上構建貓鼬網絡服務器?
- 4. 如何在貓鼬網絡服務器上保持鏈接活着
- 5. 我無法編譯這個貓鼬網絡服務器的示例代碼
- 6. 貓鼬findbyId內部服務器錯誤
- 7. Web服務器配置 - shttpd /貓鼬
- 8. 在c + +中的貓鼬服務器
- 9. 從貓鼬服務器下載tar.gz
- 10. 貓鼬服務器回調循環
- 11. 多進程網絡服務器vs多線程網絡服務器?
- 12. NowJs服務器到網絡服務器
- 13. 網絡服務和服務器程序的Android位置感知應用程序
- 14. 網絡服務器虛擬網絡
- 15. 使用貓鼬服務器端排序(mongodb + node.js)
- 16. iPhone應用程序和網絡服務
- 17. iphone網絡服務應用程序
- 18. Nodejs單線程和網絡服務器
- 19. MVC2的流程網絡服務器
- 20. 無法通過網絡服務器使用網絡服務
- 21. 秩序的網絡服務
- 22. 從網絡服務POST到服務器
- 23. 網絡服務和網絡應用程序
- 24. Apache網絡服務器
- 25. 訪問網絡服務器
- 26. 通過網絡服務器
- 27. C# - 網絡與服務器
- 28. 從網絡服務器EMAIL
- 29. 網絡服務器訪問
- 30. Android網絡服務器
你有沒有在實例(HTTP看着。 google.com/p/mongoose/source/browse/#hg%2Fexamples)目錄?這裏有一個聊天示例,展示瞭如何嵌入貓鼬並在其上構建。 – 2011-02-23 16:27:20