Colin Hogben
/
socket-test
Socket server test
Diff: main.cpp
- Revision:
- 5:2c2f993df16b
- Parent:
- 4:19dd8a25cc8a
- Child:
- 6:9c7707692925
--- a/main.cpp Thu Aug 25 18:43:08 2016 +0100 +++ b/main.cpp Thu Aug 25 21:58:09 2016 +0100 @@ -1,107 +1,112 @@ -#include "mbed.h" -#include "EthernetInterface.h" -#include "lwip/stats.h" - -#ifndef SRV_PORT -#define SRV_PORT 8084 -#endif - -Serial pc(USBTX, USBRX); -EthernetInterface eth; - -Thread stats_thread; - -void stats_main(void) -{ - int i; - while (true) { - int c = pc.getc(); - switch (c) { - default: printf("lwip statistics: *=all" - " l=LINK a=ETHARP" - " i=IP f=IPFRAG c=ICMP g=IGMP" - " u=UDP t=TCP" - " h=HEAP m=MEMP* p=PCB n=NETBUF/NETCONN" - " s=SYS\n"); break; - case '*': stats_display(); break; - case 'l': LINK_STATS_DISPLAY(); break; - case 'a': ETHARP_STATS_DISPLAY(); break; - case 'f': IPFRAG_STATS_DISPLAY(); break; - case 'i': IP_STATS_DISPLAY(); break; - case 'c': ICMP_STATS_DISPLAY(); break; - case 'g': IGMP_STATS_DISPLAY(); break; - case 'u': UDP_STATS_DISPLAY(); break; - case 't': TCP_STATS_DISPLAY(); break; - case 'h': MEM_STATS_DISPLAY(); break; - case 'm': for (i=0; i < MEMP_MAX; i++) MEMP_STATS_DISPLAY(i); break; - case 'p': - MEMP_STATS_DISPLAY(MEMP_UDP_PCB); - MEMP_STATS_DISPLAY(MEMP_TCP_PCB); - MEMP_STATS_DISPLAY(MEMP_TCP_PCB_LISTEN); - break; - case 'n': - MEMP_STATS_DISPLAY(MEMP_NETBUF); - MEMP_STATS_DISPLAY(MEMP_NETCONN); - break; - case 's': SYS_STATS_DISPLAY(); break; - } - } -} - -void tcp_echo_conn(TCPSocket *sock) -{ - static char buf[5000]; - int ret; - while (true) { - ret = sock->recv(buf, sizeof(buf)); - if (ret < 0) { - error("recv: %d", ret); - } else if (ret == 0) { - printf("recv: EOF\r\n"); - delete sock; - break; - } else { - ret = sock->send(buf, ret); - if (ret < 0) { - error("send: %d", ret); - } - } - } -} - -void tcp_echo(TCPServer *srv) -{ - int err; - while (true) { - TCPSocket *sock = new TCPSocket; - err = srv->accept(sock); - if (err) error("accept: %d\r\n", err); - printf("accepted connection\r\n"); - Thread *conn_thread = new Thread; - conn_thread->start(Callback<void()>(sock, tcp_echo_conn)); - } -} - -int main(void) -{ - int err; - printf("\r\n== socket-test %s ==\r\n", __DATE__); - stats_init(); - stats_thread.start(stats_main); - err = eth.connect(); - if (err) error("connect: %d\n", err); - printf("ip %s\r\n", eth.get_ip_address()); - TCPServer srv; - err = srv.open(ð); - if (err) error("open: %d\r\n", err); - err = srv.bind(SRV_PORT); - if (err) error("bind: %d\r\n", err); - err = srv.listen(0); - if (err) error("listen: %d\r\n", err); - Thread srv_thread; - srv_thread.start(Callback<void()>(&srv, tcp_echo)); - - while (true) { - wait(1.0); - } -} +#include "mbed.h" +#include "EthernetInterface.h" +#include "lwip/stats.h" + +#ifndef SRV_PORT +#define SRV_PORT 8084 +#endif + +Serial pc(USBTX, USBRX); +EthernetInterface eth; + +Thread console_thread; + +void console_main(void) +{ + int i; + while (true) { + int c = pc.getc(); + switch (c) { + default: printf("lwip statistics: *=all" + " l=LINK a=ETHARP" + " i=IP f=IPFRAG c=ICMP g=IGMP" + " u=UDP t=TCP" + " h=HEAP m=MEMP* p=PCB n=NETBUF/NETCONN" + " s=SYS\n"); break; + case '*': stats_display(); break; + case 'l': LINK_STATS_DISPLAY(); break; + case 'a': ETHARP_STATS_DISPLAY(); break; + case 'f': IPFRAG_STATS_DISPLAY(); break; + case 'i': IP_STATS_DISPLAY(); break; + case 'c': ICMP_STATS_DISPLAY(); break; + case 'g': IGMP_STATS_DISPLAY(); break; + case 'u': UDP_STATS_DISPLAY(); break; + case 't': TCP_STATS_DISPLAY(); break; + case 'h': MEM_STATS_DISPLAY(); break; + case 'm': for (i=0; i < MEMP_MAX; i++) MEMP_STATS_DISPLAY(i); break; + case 'p': + MEMP_STATS_DISPLAY(MEMP_UDP_PCB); + MEMP_STATS_DISPLAY(MEMP_TCP_PCB); + MEMP_STATS_DISPLAY(MEMP_TCP_PCB_LISTEN); + break; + case 'n': + MEMP_STATS_DISPLAY(MEMP_NETBUF); + MEMP_STATS_DISPLAY(MEMP_NETCONN); + break; + case 's': SYS_STATS_DISPLAY(); break; + } + } +} + +void tcp_echo_conn(TCPSocket *sock) +{ + static char buf[5000]; + int ret; + while (true) { + ret = sock->recv(buf, sizeof(buf)); + if (ret < 0) { + error("recv: %d", ret); + } else if (ret == 0) { + printf("recv: EOF\r\n"); + delete sock; + break; + } else { + putchar('<'); fflush(stdout); + ret = sock->send(buf, ret); + if (ret < 0) { + error("send: %d", ret); + } else if (ret == 0) { + error("send==0"); + } else { + putchar('>'); fflush(stdout); + } + } + } +} + +void tcp_echo(TCPServer *srv) +{ + int err; + while (true) { + TCPSocket *sock = new TCPSocket; + err = srv->accept(sock); + if (err) error("accept: %d\r\n", err); + printf("accepted connection\r\n"); + Thread *conn_thread = new Thread; + conn_thread->start(Callback<void()>(sock, tcp_echo_conn)); + } +} + +int main(void) +{ + int err; + printf("\r\n== socket-test %s ==\r\n", __DATE__); + stats_init(); + console_thread.start(console_main); + err = eth.connect(); + if (err) error("connect: %d\n", err); + printf("ip %s\r\n", eth.get_ip_address()); + TCPServer srv; + err = srv.open(ð); + if (err) error("open: %d\r\n", err); + err = srv.bind(SRV_PORT); + if (err) error("bind: %d\r\n", err); + err = srv.listen(0); + if (err) error("listen: %d\r\n", err); + Thread srv_thread; + srv_thread.start(Callback<void()>(&srv, tcp_echo)); + + while (true) { + wait(1.0); + } +}