![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Socket server test
main.cpp@5:2c2f993df16b, 2016-08-25 (annotated)
- Committer:
- Colin Hogben
- Date:
- Thu Aug 25 21:58:09 2016 +0100
- Revision:
- 5:2c2f993df16b
- Parent:
- 4:19dd8a25cc8a
- Child:
- 6:9c7707692925
Log input/output packets
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Colin Hogben |
5:2c2f993df16b | 1 | #include "mbed.h" |
Colin Hogben |
5:2c2f993df16b | 2 | #include "EthernetInterface.h" |
Colin Hogben |
5:2c2f993df16b | 3 | #include "lwip/stats.h" |
Colin Hogben |
5:2c2f993df16b | 4 | |
Colin Hogben |
5:2c2f993df16b | 5 | #ifndef SRV_PORT |
Colin Hogben |
5:2c2f993df16b | 6 | #define SRV_PORT 8084 |
Colin Hogben |
5:2c2f993df16b | 7 | #endif |
Colin Hogben |
5:2c2f993df16b | 8 | |
Colin Hogben |
5:2c2f993df16b | 9 | Serial pc(USBTX, USBRX); |
Colin Hogben |
5:2c2f993df16b | 10 | EthernetInterface eth; |
Colin Hogben |
5:2c2f993df16b | 11 | |
Colin Hogben |
5:2c2f993df16b | 12 | Thread console_thread; |
Colin Hogben |
5:2c2f993df16b | 13 | |
Colin Hogben |
5:2c2f993df16b | 14 | void console_main(void) |
Colin Hogben |
5:2c2f993df16b | 15 | { |
Colin Hogben |
5:2c2f993df16b | 16 | int i; |
Colin Hogben |
5:2c2f993df16b | 17 | while (true) { |
Colin Hogben |
5:2c2f993df16b | 18 | int c = pc.getc(); |
Colin Hogben |
5:2c2f993df16b | 19 | switch (c) { |
Colin Hogben |
5:2c2f993df16b | 20 | default: printf("lwip statistics: *=all" |
Colin Hogben |
5:2c2f993df16b | 21 | " l=LINK a=ETHARP" |
Colin Hogben |
5:2c2f993df16b | 22 | " i=IP f=IPFRAG c=ICMP g=IGMP" |
Colin Hogben |
5:2c2f993df16b | 23 | " u=UDP t=TCP" |
Colin Hogben |
5:2c2f993df16b | 24 | " h=HEAP m=MEMP* p=PCB n=NETBUF/NETCONN" |
Colin Hogben |
5:2c2f993df16b | 25 | " s=SYS\n"); break; |
Colin Hogben |
5:2c2f993df16b | 26 | case '*': stats_display(); break; |
Colin Hogben |
5:2c2f993df16b | 27 | case 'l': LINK_STATS_DISPLAY(); break; |
Colin Hogben |
5:2c2f993df16b | 28 | case 'a': ETHARP_STATS_DISPLAY(); break; |
Colin Hogben |
5:2c2f993df16b | 29 | case 'f': IPFRAG_STATS_DISPLAY(); break; |
Colin Hogben |
5:2c2f993df16b | 30 | case 'i': IP_STATS_DISPLAY(); break; |
Colin Hogben |
5:2c2f993df16b | 31 | case 'c': ICMP_STATS_DISPLAY(); break; |
Colin Hogben |
5:2c2f993df16b | 32 | case 'g': IGMP_STATS_DISPLAY(); break; |
Colin Hogben |
5:2c2f993df16b | 33 | case 'u': UDP_STATS_DISPLAY(); break; |
Colin Hogben |
5:2c2f993df16b | 34 | case 't': TCP_STATS_DISPLAY(); break; |
Colin Hogben |
5:2c2f993df16b | 35 | case 'h': MEM_STATS_DISPLAY(); break; |
Colin Hogben |
5:2c2f993df16b | 36 | case 'm': for (i=0; i < MEMP_MAX; i++) MEMP_STATS_DISPLAY(i); break; |
Colin Hogben |
5:2c2f993df16b | 37 | case 'p': |
Colin Hogben |
5:2c2f993df16b | 38 | MEMP_STATS_DISPLAY(MEMP_UDP_PCB); |
Colin Hogben |
5:2c2f993df16b | 39 | MEMP_STATS_DISPLAY(MEMP_TCP_PCB); |
Colin Hogben |
5:2c2f993df16b | 40 | MEMP_STATS_DISPLAY(MEMP_TCP_PCB_LISTEN); |
Colin Hogben |
5:2c2f993df16b | 41 | break; |
Colin Hogben |
5:2c2f993df16b | 42 | case 'n': |
Colin Hogben |
5:2c2f993df16b | 43 | MEMP_STATS_DISPLAY(MEMP_NETBUF); |
Colin Hogben |
5:2c2f993df16b | 44 | MEMP_STATS_DISPLAY(MEMP_NETCONN); |
Colin Hogben |
5:2c2f993df16b | 45 | break; |
Colin Hogben |
5:2c2f993df16b | 46 | case 's': SYS_STATS_DISPLAY(); break; |
Colin Hogben |
5:2c2f993df16b | 47 | } |
Colin Hogben |
5:2c2f993df16b | 48 | } |
Colin Hogben |
5:2c2f993df16b | 49 | } |
Colin Hogben |
5:2c2f993df16b | 50 | |
Colin Hogben |
5:2c2f993df16b | 51 | void tcp_echo_conn(TCPSocket *sock) |
Colin Hogben |
5:2c2f993df16b | 52 | { |
Colin Hogben |
5:2c2f993df16b | 53 | static char buf[5000]; |
Colin Hogben |
5:2c2f993df16b | 54 | int ret; |
Colin Hogben |
5:2c2f993df16b | 55 | while (true) { |
Colin Hogben |
5:2c2f993df16b | 56 | ret = sock->recv(buf, sizeof(buf)); |
Colin Hogben |
5:2c2f993df16b | 57 | if (ret < 0) { |
Colin Hogben |
5:2c2f993df16b | 58 | error("recv: %d", ret); |
Colin Hogben |
5:2c2f993df16b | 59 | } else if (ret == 0) { |
Colin Hogben |
5:2c2f993df16b | 60 | printf("recv: EOF\r\n"); |
Colin Hogben |
5:2c2f993df16b | 61 | delete sock; |
Colin Hogben |
5:2c2f993df16b | 62 | break; |
Colin Hogben |
5:2c2f993df16b | 63 | } else { |
Colin Hogben |
5:2c2f993df16b | 64 | putchar('<'); fflush(stdout); |
Colin Hogben |
5:2c2f993df16b | 65 | ret = sock->send(buf, ret); |
Colin Hogben |
5:2c2f993df16b | 66 | if (ret < 0) { |
Colin Hogben |
5:2c2f993df16b | 67 | error("send: %d", ret); |
Colin Hogben |
5:2c2f993df16b | 68 | } else if (ret == 0) { |
Colin Hogben |
5:2c2f993df16b | 69 | error("send==0"); |
Colin Hogben |
5:2c2f993df16b | 70 | } else { |
Colin Hogben |
5:2c2f993df16b | 71 | putchar('>'); fflush(stdout); |
Colin Hogben |
5:2c2f993df16b | 72 | } |
Colin Hogben |
5:2c2f993df16b | 73 | } |
Colin Hogben |
5:2c2f993df16b | 74 | } |
Colin Hogben |
5:2c2f993df16b | 75 | } |
Colin Hogben |
5:2c2f993df16b | 76 | |
Colin Hogben |
5:2c2f993df16b | 77 | void tcp_echo(TCPServer *srv) |
Colin Hogben |
5:2c2f993df16b | 78 | { |
Colin Hogben |
5:2c2f993df16b | 79 | int err; |
Colin Hogben |
5:2c2f993df16b | 80 | while (true) { |
Colin Hogben |
5:2c2f993df16b | 81 | TCPSocket *sock = new TCPSocket; |
Colin Hogben |
5:2c2f993df16b | 82 | err = srv->accept(sock); |
Colin Hogben |
5:2c2f993df16b | 83 | if (err) error("accept: %d\r\n", err); |
Colin Hogben |
5:2c2f993df16b | 84 | printf("accepted connection\r\n"); |
Colin Hogben |
5:2c2f993df16b | 85 | Thread *conn_thread = new Thread; |
Colin Hogben |
5:2c2f993df16b | 86 | conn_thread->start(Callback<void()>(sock, tcp_echo_conn)); |
Colin Hogben |
5:2c2f993df16b | 87 | } |
Colin Hogben |
5:2c2f993df16b | 88 | } |
Colin Hogben |
5:2c2f993df16b | 89 | |
Colin Hogben |
5:2c2f993df16b | 90 | int main(void) |
Colin Hogben |
5:2c2f993df16b | 91 | { |
Colin Hogben |
5:2c2f993df16b | 92 | int err; |
Colin Hogben |
5:2c2f993df16b | 93 | printf("\r\n== socket-test %s ==\r\n", __DATE__); |
Colin Hogben |
5:2c2f993df16b | 94 | stats_init(); |
Colin Hogben |
5:2c2f993df16b | 95 | console_thread.start(console_main); |
Colin Hogben |
5:2c2f993df16b | 96 | err = eth.connect(); |
Colin Hogben |
5:2c2f993df16b | 97 | if (err) error("connect: %d\n", err); |
Colin Hogben |
5:2c2f993df16b | 98 | printf("ip %s\r\n", eth.get_ip_address()); |
Colin Hogben |
5:2c2f993df16b | 99 | TCPServer srv; |
Colin Hogben |
5:2c2f993df16b | 100 | err = srv.open(ð); |
Colin Hogben |
5:2c2f993df16b | 101 | if (err) error("open: %d\r\n", err); |
Colin Hogben |
5:2c2f993df16b | 102 | err = srv.bind(SRV_PORT); |
Colin Hogben |
5:2c2f993df16b | 103 | if (err) error("bind: %d\r\n", err); |
Colin Hogben |
5:2c2f993df16b | 104 | err = srv.listen(0); |
Colin Hogben |
5:2c2f993df16b | 105 | if (err) error("listen: %d\r\n", err); |
Colin Hogben |
5:2c2f993df16b | 106 | Thread srv_thread; |
Colin Hogben |
5:2c2f993df16b | 107 | srv_thread.start(Callback<void()>(&srv, tcp_echo)); |
Colin Hogben |
5:2c2f993df16b | 108 | |
Colin Hogben |
5:2c2f993df16b | 109 | while (true) { |
Colin Hogben |
5:2c2f993df16b | 110 | wait(1.0); |
Colin Hogben |
5:2c2f993df16b | 111 | } |
Colin Hogben |
5:2c2f993df16b | 112 | } |