Statistics
| Branch: | Tag: | Revision:

mongoose / examples / tcp_echo_server / echo_server.c @ eaef5bd1

History | View | Annotate | Download (1.43 KB)

1
// Copyright (c) 2014 Cesanta Software Limited
2
// All rights reserved
3
//
4
// This software is dual-licensed: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License version 2 as
6
// published by the Free Software Foundation. For the terms of this
7
// license, see <http://www.gnu.org/licenses/>.
8
//
9
// You are free to use this software under the terms of the GNU General
10
// Public License, but WITHOUT ANY WARRANTY; without even the implied
11
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
// See the GNU General Public License for more details.
13
//
14
// Alternatively, you can license this software under a commercial
15
// license, as set out in <https://www.cesanta.com/license>.
16
//
17
// $Date: 2014-09-28 05:04:41 UTC $
18

    
19
#include "mongoose.h"
20

    
21
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
22
  struct mbuf *io = &nc->recv_mbuf;
23
  (void) p;
24

    
25
  switch (ev) {
26
    case MG_EV_RECV:
27
      mg_send(nc, io->buf, io->len);  // Echo message back
28
      mbuf_remove(io, io->len);       // Discard message from recv buffer
29
      break;
30
    default:
31
      break;
32
  }
33
}
34

    
35
int main(void) {
36
  struct mg_mgr mgr;
37
  const char *port1 = "1234", *port2 = "127.0.0.1:17000";
38

    
39
  mg_mgr_init(&mgr, NULL);
40
  mg_bind(&mgr, port1, ev_handler);
41
  mg_bind(&mgr, port2, ev_handler);
42

    
43
  printf("Starting echo mgr on ports %s, %s\n", port1, port2);
44
  for (;;) {
45
    mg_mgr_poll(&mgr, 1000);
46
  }
47
  mg_mgr_free(&mgr);
48

    
49
  return 0;
50
}