zoukankan      html  css  js  c++  java
  • (OK) mongoose—web server—Android——genymotion


    /opt/android-on-linux/mongoose/mongoose-android-x86/mongoose-master/mongoose.h


    #include <arpa/inet.h>
    #include <assert.h>
    #include <ctype.h>
    #include <dirent.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <inttypes.h>
    #include <limits.h>
    #include <math.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <pthread.h>
    #include <signal.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/select.h>
    #include <sys/stat.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <unistd.h>


    changing the order of the includes, that is <sys/types.h>



    #include <sys/types.h>
    #include <arpa/inet.h>
    #include <assert.h>
    #include <ctype.h>
    #include <dirent.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <inttypes.h>
    #include <limits.h>
    #include <math.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <pthread.h>
    #include <signal.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/select.h>
    #include <sys/stat.h>
    #include <sys/time.h>
    #include <unistd.h>

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    /opt/android-on-linux/android-ndk-r10d/platforms/android-21/arch-x86/usr/include/stdint.h


    # // add the red line

    #define _STLP_NATIVE_C_HEADER(header) <../include/header>

    #if defined (_STLP_HAS_INCLUDE_NEXT)
    #  include_next <stddef.h>
    #else
    #  include _STLP_NATIVE_C_HEADER(stddef.h)
    #endif

    # // otherwise, show the following error

    /opt/android-on-linux/android-ndk-r10d/sources/cxx-stl/stlport/stlport/stddef.h:35:12: 错误:#include 需要 "FILENAME" 或 <FILENAME>
     #  include _STLP_NATIVE_C_HEADER(stddef.h)


    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    /opt/android-on-linux/mongoose/mongoose-android-x86/mongoose-master/examples/examples.mk

    #NDK_ROOT = /opt/android-on-linux/android-ndk-r10d
    #SYSROOT = $(NDK_ROOT)/platforms/android-21/arch-x86/
    #CFLAGS = --sysroot=$(SYSROOT) --pipe -I$(NDK_ROOT)/sources/cxx-stl/stlport/stlport
    #CFLAGS += -I$(NDK_ROOT)/platforms/android-21/arch-x86/usr/include
    #CFLAGS += -I$(NDK_ROOT)/toolchains/x86-4.9/prebuilt/linux-x86_64/include
    #CFLAGS += -I$(NDK_ROOT)/toolchains/x86-4.9/prebuilt/linux-x86_64/lib/gcc/i686-linux-android/4.9/include-fixed
    #CFLAGS += -I$(NDK_ROOT)/toolchains/x86-4.9/prebuilt/linux-x86_64/lib/gcc/i686-linux-android/4.9/include
    
    #CPATH = /opt/android-on-linux/android-ndk-r10d/toolchains/x86-4.9/prebuilt/linux-x86_64/bin/
    
    #CFLAGS += -g -W -Wall -I../.. -Wno-unused-function $(CFLAGS_EXTRA) $(MODULE_CFLAGS)
    
    #CFLAGS += -Wl,-rpath-link=$(NDK_ROOT)/platforms/android-21/arch-x86/usr/lib/
    #CFLAGS += -L$(NDK_ROOT)/platforms/android-21/arch-x86/usr/lib/
    
    # refer to http://bodudu.org/2011/04/porting-mongoose-web-server-to-android/
    NDK_HOME = /opt/android-on-linux/android-ndk-r10d
    TOOLCHAIN_HOME = $(NDK_HOME)/toolchains/x86-4.9/prebuilt/linux-x86_64
    CROSS_COMPILE = $(TOOLCHAIN_HOME)/bin/i686-linux-android-
    SYSROOT = $(NDK_HOME)/platforms/android-21/arch-x86
    CC = $(CROSS_COMPILE)gcc
    NDK_LIB = $(SYSROOT)/usr/lib 
    
    #error: only position independent executables (PIE) are supported. (-fPIE)
    CFLAGS += -g -W -fPIE -Wall -I../.. -Wno-unused-function
    
    #CCFLAGS=-MMD -MP -fpic -ffunction-sections -funwind-tables -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__  -Wno-psabi -march=armv5te -mtune=xscale -msoft-float -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -DANDROID -Wa,–noexecstack -g
    #CCFLAGS=
    INCDIRS = -I$(SYSROOT)/usr/include
    LDFLAGS = --sysroot=$(SYSROOT) -L$(NDK_LIB)
    #LDFLAGS += -Wl,–gc-sections -Wl,-z,nocopyreloc -Wl,–no-undefined -Wl,-z,noexecstack -Wl,-rpath-link=$(SYSROOT)/usr/lib
    LDFLAGS += -Wl,-rpath-link=$(SYSROOT)/usr/lib
    
    #error: only position independent executables (PIE) are supported. (-pie)
    LDFLAGS += -ldl -pie
    
    
    SOURCES = $(PROG).c ../../mongoose.c
    
    all: $(PROG)
    
    ifeq ($(OS), Windows_NT)
    # TODO(alashkin): enable SSL in Windows
    CFLAGS += -lws2_32
    # CC = gcc
    #CC = $(CPATH)/i686-linux-android-gcc
    #LD = $(CPATH)/i686-linux-android-ld
    
    else
    ifeq ($(SSL_LIB),openssl)
    CFLAGS += -DMG_ENABLE_SSL -lssl -lcrypto
    else ifeq ($(SSL_LIB), krypton)
    CFLAGS += -DMG_ENABLE_SSL -DMG_DISABLE_PFS ../../../krypton/krypton.c
    endif
    # CFLAGS += -lpthread
    endif
    
    ifeq ($(JS), yes)
    	V7_PATH = ../../deps/v7
    	CFLAGS_EXTRA += -DMG_ENABLE_JAVASCRIPT -I $(V7_PATH) $(V7_PATH)/v7.c
    endif
    
    $(PROG): $(SOURCES)
    	$(CC) $(SOURCES) -o $@ $(CFLAGS) $(INCDIRS) $(LDFLAGS)
    
    $(PROG).exe: $(SOURCES)
    	cl $(SOURCES) /I../.. /MD /Fe$@
    
    clean:
    	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
    




    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    simplest_web_server.c

    // Copyright (c) 2015 Cesanta Software Limited
    // All rights reserved
    
    #include "mongoose.h"
    
    static const char *s_http_port = "80";
    static struct mg_serve_http_opts s_http_server_opts;
    
    static void ev_handler(struct mg_connection *nc, int ev, void *p) {
      if (ev == MG_EV_HTTP_REQUEST) {
        mg_serve_http(nc, (struct http_message *) p, s_http_server_opts);
      }
    }
    
    int main(void) {
      struct mg_mgr mgr;
      struct mg_connection *nc;
    
      mg_mgr_init(&mgr, NULL);
      nc = mg_bind(&mgr, s_http_port, ev_handler);
    
      // Set up HTTP server parameters
      mg_set_protocol_http_websocket(nc);
      s_http_server_opts.document_root = ".";  // Serve current directory
      s_http_server_opts.dav_document_root = ".";  // Allow access via WebDav
      s_http_server_opts.enable_directory_listing = "yes";
    
      printf("Starting web server on port %s
    ", s_http_port);
      for (;;) {
        mg_mgr_poll(&mgr, 1000);
      }
      mg_mgr_free(&mgr);
    
      return 0;
    }
    



    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    websocket_chat.c

    /*
     * Copyright (c) 2014 Cesanta Software Limited
     * All rights reserved
     */
    
    #include "mongoose.h"
    
    static sig_atomic_t s_signal_received = 0;
    static const char *s_http_port = "8000";
    static struct mg_serve_http_opts s_http_server_opts;
    
    static void signal_handler(int sig_num) {
      signal(sig_num, signal_handler);  // Reinstantiate signal handler
      s_signal_received = sig_num;
    }
    
    static int is_websocket(const struct mg_connection *nc) {
      return nc->flags & MG_F_IS_WEBSOCKET;
    }
    
    static void broadcast(struct mg_connection *nc, const struct mg_str msg) {
      struct mg_connection *c;
      char buf[500];
      char addr[32];
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
    
      snprintf(buf, sizeof(buf), "%s %.*s", addr, (int) msg.len, msg.p);
      printf("%s
    ", buf); /* Local echo. */
      for (c = mg_next(nc->mgr, NULL); c != NULL; c = mg_next(nc->mgr, c)) {
        if (c == nc) continue; /* Don't send to the sender. */
        mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, buf, strlen(buf));
      }
    }
    
    static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
      switch (ev) {
        case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
          /* New websocket connection. Tell everybody. */
          broadcast(nc, mg_mk_str("++ joined"));
          break;
        }
        case MG_EV_WEBSOCKET_FRAME: {
          struct websocket_message *wm = (struct websocket_message *) ev_data;
          /* New websocket message. Tell everybody. */
          struct mg_str d = {(char *) wm->data, wm->size};
          broadcast(nc, d);
          break;
        }
        case MG_EV_CLOSE: {
          /* Disconnect. Tell everybody. */
          if (is_websocket(nc)) {
            broadcast(nc, mg_mk_str("-- left"));
          }
          break;
        }
      }
    }
    
    int main(void) {
      struct mg_mgr mgr;
      struct mg_connection *nc;
    
      signal(SIGTERM, signal_handler);
      signal(SIGINT, signal_handler);
      setvbuf(stdout, NULL, _IOLBF, 0);
      setvbuf(stderr, NULL, _IOLBF, 0);
    
      mg_mgr_init(&mgr, NULL);
    
      nc = mg_bind(&mgr, s_http_port, ev_handler);
      s_http_server_opts.document_root = ".";
      mg_set_protocol_http_websocket(nc);
    
      printf("Started on port %s
    ", s_http_port);
      while (s_signal_received == 0) {
        mg_mgr_poll(&mgr, 200);
      }
      mg_mgr_free(&mgr);
    
      return 0;
    }
    



    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    [root@localhost websocket_chat]# ls
    index.html  Makefile  websocket_chat  websocket_chat.c
    [root@localhost websocket_chat]# gedit index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8" />
      <title>WebSocket Test</title>
      <meta name="viewport" content="width=device-width, initial-scale=1" />
      <style type="text/css">
        body {
          background-color: #789; margin: 0;
          padding: 0; font: 14px Helvetica, Arial, sans-serif;
        }
        div.content {
           800px; margin: 2em auto; padding: 20px 50px;
          background-color: #fff; border-radius: 1em;
        }
        #messages {
          border: 2px solid #fec; border-radius: 1em;
          height: 10em; overflow: scroll; padding: 0.5em 1em;
        }
        a:link, a:visited { color: #69c; text-decoration: none; }
        @media (max- 700px) {
          body { background-color: #fff; }
          div.content {
             auto; margin: 0 auto; border-radius: 0;
            padding: 1em;
          }
        }
    </style>
    
    <script language="javascript" type="text/javascript">
    
      var rooms = [];
      //var ws = new WebSocket('ws://' + location.host + '/ws');
      var ws = new WebSocket('ws://' + location.host + ':8000');
    
      if (!window.console) { window.console = { log: function() {} } };
    
      ws.onopen = function(ev)  { console.log(ev); };
      ws.onerror = function(ev) { console.log(ev); };
      ws.onclose = function(ev) { console.log(ev); };
      ws.onmessage = function(ev) {
        console.log(ev);
        var div = document.createElement('div');
        div.innerHTML = ev.data;
        document.getElementById('messages').appendChild(div);
    
      };
    
      window.onload = function() {
        document.getElementById('send_button').onclick = function(ev) {
          var msg = document.getElementById('send_input').value;
          document.getElementById('send_input').value = '';
          ws.send(msg);
        };
        document.getElementById('send_input').onkeypress = function(ev) {
          if (ev.keyCode == 13 || ev.which == 13) {
            document.getElementById('send_button').click();
          }
        };
      };
    </script>
    </head>
    <body>
      <div class="content">
        <h1>Websocket PubSub Demonstration</h1>
    
        <p>
          This page demonstrates how Mongoose could be used to implement
          <a href="http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern">
           publish–subscribe pattern</a>. Open this page in several browser
           windows. Each window initiates persistent
           <a href="http://en.wikipedia.org/wiki/WebSocket">WebSocket</a>
          connection with the server, making each browser window a websocket client.
          Send messages, and see messages sent by other clients.
        </p>
    
        <div id="messages">
        </div>
    
        <p>
          <input type="text" id="send_input" />
          <button id="send_button">Send Message</button>
        </p>
      </div>
    </body>
    </html>
    



    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    [root@localhost simplest_web_server]# make
    [root@localhost simplest_web_server]# pwd
    /opt/android-on-linux/mongoose/mongoose-android-x86/mongoose-master/examples/simplest_web_server
    [root@localhost simplest_web_server]# ls
    Makefile  simplest_web_server  simplest_web_server.c
    [root@localhost simplest_web_server]# adb push simplest_web_server /data/webserver


    [root@localhost websocket_chat]# make
    [root@localhost websocket_chat]# ls
    index.html  Makefile  websocket_chat  websocket_chat.c
    [root@localhost websocket_chat]# pwd
    /opt/android-on-linux/mongoose/mongoose-android-x86/mongoose-master/examples/websocket_chat
    [root@localhost websocket_chat]# adb push websocket_chat /data/webserver
    [root@localhost websocket_chat]# adb push index.html /data/webserver


    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    root@vbox86p:/data/webserver # netcfg                                          
    eth0     UP                              192.168.56.102/24  0x00001043 08:00:27:a5:c4:5c
    eth1     UP                                   10.0.3.15/24  0x00001043 08:00:27:b0:24:2e
    ifb0     DOWN                                   0.0.0.0/0   0x00000082 66:65:84:20:d3:7d
    ifb1     DOWN                                   0.0.0.0/0   0x00000082 2e:3e:58:3d:92:d7
    sit0     DOWN                                   0.0.0.0/0   0x00000080 00:00:00:00:00:00
    lo       UP                                   127.0.0.1/8   0x00000049 00:00:00:00:00:00
    root@vbox86p:/data/webserver #


    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


  • 相关阅读:
    解决php网页运行超时问题:Maximum execution time of 30 seconds exceeded
    C#操作Xml:XSLT语法 在.net中使用XSLT转换xml文档示例
    C#操作Xml:XPath语法 在C#中使用XPath示例
    C#操作Xml:XmlSerializer 对象的Xml序列化和反序列化
    C#操作Xml:如何定义Xsd文件
    C#操作Xml:linq to xml操作XML
    C#操作Xml:使用XmlWriter写Xml
    C#操作Xml:使用XmlReader读Xml
    C#操作Xml:通过XmlDocument读写Xml文档
    XML的序列化和反序列化 详细介绍
  • 原文地址:https://www.cnblogs.com/ztguang/p/12646492.html
Copyright © 2011-2022 走看看