我遇到了一个名为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. 如何在猫鼬网络服务器上保持链接活着
- 3. Web服务器配置 - shttpd /猫鼬
- 4. 猫鼬findbyId内部服务器错误
- 5. 在c + +中的猫鼬服务器
- 6. 从猫鼬服务器下载tar.gz
- 7. 多进程网络服务器vs多线程网络服务器?
- 8. NowJs服务器到网络服务器
- 9. 网络服务和服务器程序的Android位置感知应用程序
- 10. 网络服务器虚拟网络
- 11. 使用猫鼬服务器端排序(mongodb + node.js)
- 12. iPhone应用程序和网络服务
- 13. MVC2的流程网络服务器
- 14. 无法通过网络服务器使用网络服务
- 15. 秩序的网络服务
- 16. 网络服务和网络应用程序
- 17. 网络应用服务器
- 18. Android网络服务器
- 19. C# - 网络与服务器
- 20. API的网络服务器
- 21. 从网络服务器EMAIL
- 22. 网络服务器认证
- 23. Red5:服务器应用程序框架和helloworld
- 24. 猫鼬:排序
- 25. 网络服务
- 26. SharePoint网络服务“无法连接到远程服务器”
- 27. 没有网络服务器的AngularJS应用程序
- 28. 将数据存储到网络服务器的VB.Net程序
- 29. 网络与Windows客户端/服务器应用程序
- 30. 网络服务器应用程序的负载测试策略
你有没有在实例(HTTP看着。 google.com/p/mongoose/source/browse/#hg%2Fexamples)目录?这里有一个聊天示例,展示了如何嵌入猫鼬并在其上构建。 – 2011-02-23 16:27:20