2011-02-23 12 views
18

मैंगोज़ और http://code.google.com/p/mongoose/ नामक एक एम्बेडेड वेब सर्वर पर आया और मैंने विकी को पढ़ा और यह बहुत अच्छा था और मैंने कुछ नमूना हैलो वर्ल्ड प्रोग्राम की खोज की लेकिन मुझे यह नहीं मिला ... मुझे कुछ उदाहरण मिला लेकिन यह है कि विंडोज़ के लिए C++ में लिखा गया था और किसी भी एक एक उदाहरण सी कार्यक्रम इस वेबसर्वर चलाने के लिए प्रदान कर सकते हैं ..mongoose वेब सर्वर helloworld प्रोग्राम

+0

आप उदाहरण (http में ध्यान दिया है। google.com/p/mongoose/source/browse/#hg%2Fexamples) निर्देशिका? एक चैट उदाहरण है जो दिखाता है कि कैसे मोन्गोज़ एम्बेड करना है और इसके शीर्ष पर बनाना है। –

उत्तर

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

मैंने एक सी ++ आरईएसटी सेवा पुस्तकालय लिखा जो मोंगोस का उपयोग करता है। यहाँ एक सरल उदाहरण है:

#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

संकलित आवेदन के आधार पर: // कोड: $ सीसी 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; 
} 
संबंधित मुद्दे