mongoose 嵌入web服务器 例子
https://blog.csdn.net/wowocpp/article/details/116206013
./settings_panel
Starting device configurator on port 8000
Started on port 8000
./publish_subscribe 8000 server
Starting pubsub server on port 8000
New client connected from 192.168.99.219:55907
New client connected from 192.168.99.219:52381
New client connected from 192.168.99.219:49577
New client connected from 192.168.99.219:51166
New client connected from 192.168.99.219:49899
通过浏览器输入如下网址:
http://192.168.99.31:8000/
Starting RESTful client against http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=cesanta
Server closed connection
RESTful server host and request URI
Starting web server on port 8000
Starting web server on port 8000
user logged in, sid 25690a82c6be15f2
user (sid 25690a82c6be15f2) requested /
Session 25690a82c6be15f2 (user) closed due to idleness.
Starting RESTful server on port 8000, serving .
MQTT broker started on 0.0.0.0:1883
USER HANDLER GOT EVENT 1
USER HANDLER GOT EVENT 3
USER HANDLER GOT EVENT 201
USER HANDLER GOT EVENT 4
USER HANDLER GOT EVENT 3
USER HANDLER GOT EVENT 203
USER HANDLER GOT EVENT 3
USER HANDLER GOT EVENT 203
USER HANDLER GOT EVENT 3
USER HANDLER GOT EVENT 203
USER HANDLER GOT EVENT 3
USER HANDLER GOT EVENT 212
win7 下面
Starting web server on port 8000
./udp_client 192.168.99.219:60000 55555
./nc 192.168.99.219:60000
/home/sama5d2/general_work/http_web_server/mongoose/mongoose-6.18/examples/websocket_chat
./websocket_chat
Started on port 8000
192.168.99.219:50468 ++ joined
192.168.99.219:50468 1111
33333
127.0.0.1:60248 ++ joined
127.0.0.1:60248 9999
192.168.99.219:50468 ok
127.0.0.1:60248 hello
websocket_chat 运行的同时
同一个机器上,运行下面的程序:
./websocket_chat_client
-- Connected
192.168.99.219:65479 ++ joined
192.168.99.219:65479 -- left
192.168.99.219:54058 ++ joined
1111
192.168.99.219:54058 55555
2个网页端 和1个客户端 之间互相通信
相当于聊天室 群聊
同步时间
static const char *s_default_server = “pool.ntp.org”;
sntp_client
Local time: Thu Apr 29 15:32:44 2021
Time from pool.ntp.org: Thu Apr 29 15:32:45 2021
mongoose-7.2/examples/websocket-server
修改
cc …/…/mongoose.c -I…/… -W -Wall -DMG_ENABLE_LINES=1 -DMG_ENABLE_DIRECTORY_LISTING=1 -o example main.c
./example
2021-04-29 08:06:00 I sock.c:469:mg_listen 1 accepting on http://0.0.0.0:8000
// Example Websocket server. Usage:
// 1. Start this server, type make
// 2. Open https://www.websocket.org/echo.html in your browser
// 3. In the “Location” text field, type ws://127.0.0.1:8000/websocket
http://coolaf.com/tool/chattest
multithreaded.c 和 websocket_chat 结合一下
或者和 nc 结合一下
mg_start_thread
mg_broadcast
都是啥意思
Starting web server on port 8000
2021-04-30 02:18:54 I mongoose.c:3056:mg_listen 1 accepting on http://0.0.0.0:8000
curl 192.168.99.31:8000/api/video1
void
mg_stop(struct mg_context *ctx)
{
ctx->stop_flag = 1;
/* Wait until mg_fini() stops */
while (ctx->stop_flag != 2)
(void) sleep(1);
assert(ctx->num_threads == 0);
free(ctx);
#if defined(_WIN32)
(void) WSACleanup();
#endif /* _WIN32 */
}
struct mg_context *
mg_start(void)
{
struct mg_context *ctx;
const struct mg_option *option;
#if defined(_WIN32)
WSADATA data;
WSAStartup(MAKEWORD(2,2), &data);
#endif /* _WIN32 */
if ((ctx = (struct mg_context *) calloc(1, sizeof(*ctx))) == NULL) {
cry(fc(ctx), "cannot allocate mongoose context");
return (NULL);
}
/* Initialize options. First pass: set default option values */
for (option = known_options; option->name != NULL; option++)
ctx->options[option->index] = option->default_value == NULL ?
NULL : mg_strdup(option->default_value);
/* Call setter functions */
for (option = known_options; option->name != NULL; option++)
if (option->setter != NULL &&
ctx->options[option->index] != NULL)
if (option->setter(ctx,
ctx->options[option->index]) == FALSE) {
mg_fini(ctx);
return (NULL);
}
DEBUG_TRACE((DEBUG_MGS_PREFIX "%s: root [%s]",
__func__, ctx->options[OPT_ROOT]));
#if !defined(_WIN32)
/*
* Ignore SIGPIPE signal, so if browser cancels the request, it
* won't kill the whole process.
*/
(void) signal(SIGPIPE, SIG_IGN);
#endif /* _WIN32 */
(void) pthread_rwlock_init(&ctx->rwlock, NULL);
(void) pthread_mutex_init(&ctx->mutex, NULL);
(void) pthread_cond_init(&ctx->thr_cond, NULL);
(void) pthread_cond_init(&ctx->empty_cond, NULL);
(void) pthread_cond_init(&ctx->full_cond, NULL);
/* Start master (listening) thread */
start_thread(ctx, (mg_thread_func_t) master_thread, ctx);
return (ctx);
}