关闭

(OK) mongoose—web server—Android——genymotion

784人阅读 评论(0) 收藏 举报
分类:


/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\n", 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\n", 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\n", 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 {
      width: 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-width: 700px) {
      body { background-color: #fff; }
      div.content {
        width: 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 #


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


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


0
0
查看评论

mongoose入门(二)

Schema如果你没有阅读 mongoose入门(一) ,就请花一分钟去看看,初步了解一下Mongoose是如何工作的。定义你的schema在Mongoose中万物皆有Schema开始。每个schema映射到MongoDB集合,并定义在集合内的文档的形状。 var mongoose = requir...
  • keliyxyz
  • keliyxyz
  • 2016-08-22 18:19
  • 903

mongoose webserver 支持 https的步骤

1. 创建证书 openssl genrsa -out privatekey.pem 1024 openssl req -new -x509 -key privatekey.pem -out publickey.cer -days 1825 2.拷贝privatekey.pem和publicke...
  • dotphoenix
  • dotphoenix
  • 2015-03-16 14:16
  • 4615

Mongoose API Reference

Mongoose API Reference struct mg_server *mg_create_server(void *server_param); Creates web server instance. Returns opaque instance pointer, or NULL ...
  • niwenxian1
  • niwenxian1
  • 2015-01-24 18:10
  • 1825

mongoose源码分析系列一

注:为何选择5.1版本进行分析,主要还是5.1版本的代码结构比较有序,功能不是太复杂。后续会继续分析5.2,5.3以及最新代码。 为了简化代码,打开下述编译宏以关闭对应的功能: NO_CGI,NO_WEBSOCKET,NO_DIRECTORY_LISTING,NO_DAV,NO_AUTH...
  • nfer_cn
  • nfer_cn
  • 2014-04-24 10:39
  • 1869

web服务器之mongoose:核心处理模块

引言 前面我们介绍了Mongoose所有的几个主要的数据结构mg_context、mg_connection、mg_request_info,还有Mongoose的生命主线。有了这些基础就可以来看看Mongoose的核心处理工作是怎样的。 本文从下面几个方面去介绍...
  • yangxuan12580
  • yangxuan12580
  • 2016-06-13 18:25
  • 679

我读Mongoose源码----程序框架

分类: Unix网络编程2013-08-22 21:19 352人阅读 评论(4) 收藏 举报 Mongoose源代码线程Unix Mongoose是一种WEB服务器,因为最近在学习网络编程,所以打算研究研究它的源码,认真看了大部分,...
  • pi9nc
  • pi9nc
  • 2014-04-07 21:17
  • 2321

mongoose之master_thread函数

mongoose采用了生产者-消费者模式来运作,即一端接受连接(生产),另一端处理连接(消费),如此反复。master_thread()函数采用了select的I/O复用模式,相信这也是有评论称mongoose效率不高的原因。因为select函数原型如下 int select(int nfds, ...
  • huolianglee
  • huolianglee
  • 2012-09-26 16:57
  • 1064

(OK) mongoose—web server—Android——genymotion

(OK) mongoose—web server—Android——genymotion
  • ztguang
  • ztguang
  • 2016-06-02 08:42
  • 784

Mongoose6.0源码分析(1)-从一个example开始到读取数据

Mongoose6.0源码分析
  • zxy355
  • zxy355
  • 2015-10-22 10:55
  • 2079

将Mongoose webserver嵌入到你的应用

http://zhiwei.li/text/2009/11/%E5%B0%86mongoose-webserver%E5%B5%8C%E5%85%A5%E5%88%B0%E4%BD%A0%E7%9A%84%E5%BA%94%E7%94%A8/ 下载下面3个文件到某个目录 _mon...
  • larryliuqing
  • larryliuqing
  • 2012-05-07 22:13
  • 1934
    个人资料
    • 访问:1699761次
    • 积分:23027
    • 等级:
    • 排名:第363名
    • 原创:1829篇
    • 转载:1045篇
    • 译文:0篇
    • 评论:68条
    相册/老博客
    最新评论