![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Socket server test
main.cpp@0:d2e0685698cc, 2016-08-23 (annotated)
- Committer:
- infinnovation
- Date:
- Tue Aug 23 19:22:03 2016 +0000
- Revision:
- 0:d2e0685698cc
- Child:
- 4:19dd8a25cc8a
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
infinnovation | 0:d2e0685698cc | 1 | #include "mbed.h" |
infinnovation | 0:d2e0685698cc | 2 | #include "EthernetInterface.h" |
infinnovation | 0:d2e0685698cc | 3 | |
infinnovation | 0:d2e0685698cc | 4 | #ifndef SRV_PORT |
infinnovation | 0:d2e0685698cc | 5 | #define SRV_PORT 8084 |
infinnovation | 0:d2e0685698cc | 6 | #endif |
infinnovation | 0:d2e0685698cc | 7 | |
infinnovation | 0:d2e0685698cc | 8 | EthernetInterface eth; |
infinnovation | 0:d2e0685698cc | 9 | |
infinnovation | 0:d2e0685698cc | 10 | void tcp_echo_conn(TCPSocket *sock) |
infinnovation | 0:d2e0685698cc | 11 | { |
infinnovation | 0:d2e0685698cc | 12 | char buf[200]; |
infinnovation | 0:d2e0685698cc | 13 | int ret; |
infinnovation | 0:d2e0685698cc | 14 | while (true) { |
infinnovation | 0:d2e0685698cc | 15 | ret = sock->recv(buf, sizeof(buf)); |
infinnovation | 0:d2e0685698cc | 16 | if (ret < 0) { |
infinnovation | 0:d2e0685698cc | 17 | error("recv: %d", ret); |
infinnovation | 0:d2e0685698cc | 18 | } else if (ret == 0) { |
infinnovation | 0:d2e0685698cc | 19 | printf("recv: EOF\r\n"); |
infinnovation | 0:d2e0685698cc | 20 | delete sock; |
infinnovation | 0:d2e0685698cc | 21 | break; |
infinnovation | 0:d2e0685698cc | 22 | } else { |
infinnovation | 0:d2e0685698cc | 23 | ret = sock->send(buf, ret); |
infinnovation | 0:d2e0685698cc | 24 | if (ret < 0) { |
infinnovation | 0:d2e0685698cc | 25 | error("send: %d", ret); |
infinnovation | 0:d2e0685698cc | 26 | } |
infinnovation | 0:d2e0685698cc | 27 | } |
infinnovation | 0:d2e0685698cc | 28 | } |
infinnovation | 0:d2e0685698cc | 29 | } |
infinnovation | 0:d2e0685698cc | 30 | |
infinnovation | 0:d2e0685698cc | 31 | void tcp_echo(TCPServer *srv) |
infinnovation | 0:d2e0685698cc | 32 | { |
infinnovation | 0:d2e0685698cc | 33 | int err; |
infinnovation | 0:d2e0685698cc | 34 | while (true) { |
infinnovation | 0:d2e0685698cc | 35 | TCPSocket *sock = new TCPSocket; |
infinnovation | 0:d2e0685698cc | 36 | err = srv->accept(sock); |
infinnovation | 0:d2e0685698cc | 37 | if (err) error("accept: %d\r\n", err); |
infinnovation | 0:d2e0685698cc | 38 | printf("accepted connection\r\n"); |
infinnovation | 0:d2e0685698cc | 39 | Thread *conn_thread = new Thread; |
infinnovation | 0:d2e0685698cc | 40 | conn_thread->start(Callback<void()>(sock, tcp_echo_conn)); |
infinnovation | 0:d2e0685698cc | 41 | } |
infinnovation | 0:d2e0685698cc | 42 | } |
infinnovation | 0:d2e0685698cc | 43 | |
infinnovation | 0:d2e0685698cc | 44 | int main(void) |
infinnovation | 0:d2e0685698cc | 45 | { |
infinnovation | 0:d2e0685698cc | 46 | int err; |
infinnovation | 0:d2e0685698cc | 47 | printf("\r\n== socket-test %s ==\r\n", __DATE__); |
infinnovation | 0:d2e0685698cc | 48 | err = eth.connect(); |
infinnovation | 0:d2e0685698cc | 49 | if (err) error("connect: %d\n", err); |
infinnovation | 0:d2e0685698cc | 50 | printf("ip %s\r\n", eth.get_ip_address()); |
infinnovation | 0:d2e0685698cc | 51 | TCPServer srv; |
infinnovation | 0:d2e0685698cc | 52 | err = srv.open(ð); |
infinnovation | 0:d2e0685698cc | 53 | if (err) error("open: %d\r\n", err); |
infinnovation | 0:d2e0685698cc | 54 | err = srv.bind(SRV_PORT); |
infinnovation | 0:d2e0685698cc | 55 | if (err) error("bind: %d\r\n", err); |
infinnovation | 0:d2e0685698cc | 56 | err = srv.listen(1); |
infinnovation | 0:d2e0685698cc | 57 | if (err) error("listen: %d\r\n", err); |
infinnovation | 0:d2e0685698cc | 58 | Thread srv_thread; |
infinnovation | 0:d2e0685698cc | 59 | srv_thread.start(Callback<void()>(&srv, tcp_echo)); |
infinnovation | 0:d2e0685698cc | 60 | |
infinnovation | 0:d2e0685698cc | 61 | while (true) { |
infinnovation | 0:d2e0685698cc | 62 | wait(1.0); |
infinnovation | 0:d2e0685698cc | 63 | } |
infinnovation | 0:d2e0685698cc | 64 | } |