シンプルな C++ Web ベースの GUI を作成しようとしています。Qt または Visual Studio ベースの GUI の使用には興味がありません。私の要件は非常に最小限で基本的なものであるため、Web ベースに興味があります。
そこで、C ベースの Web サーバーである「Mongoose」に出会いました。例を見た後、いくつかのコードを作成しましたが、インターネットプログラミングに関する知識がほとんどないため、機能しません。POST または GET 要求を使用して、HTML フォームからユーザー データを取得できる簡単な例があるかどうか疑問に思っていました。
これが私がこれまでに管理したものです:
//////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mongoose.h"
static const char *s_http_port = "8000";
volatile bool kill_server = FALSE;
struct mg_mgr mgr;
struct mg_connection *nc;
bool control1_triggered = FALSE;
bool control2_triggered = FALSE;
struct file_writer_data {
FILE *fp;
size_t bytes_written;
};
static void handle_upload(struct mg_connection *nc, int ev, void *p) {
printf("Signal received! %d\n", ev);
control1_triggered = TRUE;
struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p;
printf(mp->data.p);
switch (ev) {
case MG_EV_HTTP_PART_DATA:
break;
}
}
static void handle_upload2(struct mg_connection *nc, int ev, void *p) {
printf("Signal received@2! %d\n", ev);
control2_triggered = TRUE;
}
void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
(void)ev_data;
switch (ev) {
case MG_EV_HTTP_REQUEST:
// Invoked when the full HTTP request is in the buffer (including body).
mg_printf(nc, "%s",
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n"
"\r\n"
"<html><body>Controls"
"<form method=\"GET\" action=\"/upload\" "
" enctype=\"multipart/form-data\">"
"<input type = \"text\" name = \"fname\" value = \"John\">"
"<input type=\"submit\" value=\"Fix Position\" />"
"</form>"
"<form method=\"POST\" action=\"/Kill\" "
" enctype=\"multipart/form-data\">"
"<input type=\"submit\" value=\"Kill Server\" />"
"</form>"
"input.search{width: 20em; height: 2em;}"
"</body></html>");
nc->flags |= MG_F_SEND_AND_CLOSE;
break;
}
}
int main() {
mg_mgr_init(&mgr, NULL);
nc = mg_bind(&mgr, s_http_port, ev_handler);
mg_register_http_endpoint(nc, "/upload", handle_upload);
mg_register_http_endpoint(nc, "/Kill", handle_upload2);
// Set up HTTP server parameters
mg_set_protocol_http_websocket(nc);
while (1);
return 0;
}
私は 3 日前からグーグルで検索しており、ほとんどのリンクと質問を見てきました。しかし、Mongoose のサポートはあまり多くありません。Mongoose を使用して GET または POST HTML リクエストを解析する方法の例を教えてください。
どうもありがとう。
乾杯、アヴィ