hello.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "mongoose.h"
  4. static void *callback(enum mg_event event,
  5. struct mg_connection *conn,
  6. const struct mg_request_info *request_info) {
  7. if (event == MG_NEW_REQUEST) {
  8. char content[1024];
  9. int content_length = snprintf(content, sizeof(content),
  10. "Hello from mongoose! Remote port: %d",
  11. request_info->remote_port);
  12. mg_printf(conn,
  13. "HTTP/1.1 200 OK\r\n"
  14. "Content-Type: text/plain\r\n"
  15. "Content-Length: %d\r\n" // Always set Content-Length
  16. "\r\n"
  17. "%s",
  18. content_length, content);
  19. // Mark as processed
  20. return "";
  21. } else {
  22. return NULL;
  23. }
  24. }
  25. int main(void) {
  26. struct mg_context *ctx;
  27. const char *options[] = {"listening_ports", "8080", NULL};
  28. ctx = mg_start(&callback, NULL, options);
  29. getchar(); // Wait until user hits "enter"
  30. mg_stop(ctx);
  31. return 0;
  32. }