Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@1:59e0d83a4014, 2017-04-27 (annotated)
- Committer:
- tulanthoar
- Date:
- Thu Apr 27 16:04:35 2017 -0600
- Revision:
- 1:59e0d83a4014
- Parent:
- 0:5d1a93a692a1
- Child:
- 3:f4f66901181c
the server now streams hello over and over
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tulanthoar | 0:5d1a93a692a1 | 1 | #if !FEATURE_LWIP |
| tulanthoar | 0:5d1a93a692a1 | 2 | #error [NOT_SUPPORTED] LWIP not supported for this target |
| tulanthoar | 0:5d1a93a692a1 | 3 | #endif |
| tulanthoar | 0:5d1a93a692a1 | 4 | |
| tulanthoar | 0:5d1a93a692a1 | 5 | #include "mbed.h" |
| tulanthoar | 0:5d1a93a692a1 | 6 | #include "EthernetInterface.h" |
| tulanthoar | 0:5d1a93a692a1 | 7 | #include "TCPServer.h" |
| tulanthoar | 0:5d1a93a692a1 | 8 | #include "TCPSocket.h" |
| tulanthoar | 0:5d1a93a692a1 | 9 | |
| tulanthoar | 0:5d1a93a692a1 | 10 | #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" |
| tulanthoar | 0:5d1a93a692a1 | 11 | #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" |
| tulanthoar | 0:5d1a93a692a1 | 12 | #define HTTP_MESSAGE_BODY "" \ |
| tulanthoar | 0:5d1a93a692a1 | 13 | "<html>" "\r\n" \ |
| tulanthoar | 0:5d1a93a692a1 | 14 | " <body style=\"display:flex;text-align:center\">" "\r\n" \ |
| tulanthoar | 0:5d1a93a692a1 | 15 | " <div style=\"margin:auto\">" "\r\n" \ |
| tulanthoar | 0:5d1a93a692a1 | 16 | " <h1>Hello World</h1>" "\r\n" \ |
| tulanthoar | 0:5d1a93a692a1 | 17 | " <p>It works !</p>" "\r\n" \ |
| tulanthoar | 0:5d1a93a692a1 | 18 | " </div>" "\r\n" \ |
| tulanthoar | 0:5d1a93a692a1 | 19 | " </body>" "\r\n" \ |
| tulanthoar | 1:59e0d83a4014 | 20 | "</html>\r\n" |
| tulanthoar | 0:5d1a93a692a1 | 21 | |
| tulanthoar | 0:5d1a93a692a1 | 22 | #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ |
| tulanthoar | 0:5d1a93a692a1 | 23 | HTTP_HEADER_FIELDS "\r\n" \ |
| tulanthoar | 0:5d1a93a692a1 | 24 | "\r\n" \ |
| tulanthoar | 0:5d1a93a692a1 | 25 | HTTP_MESSAGE_BODY "\r\n" |
| tulanthoar | 0:5d1a93a692a1 | 26 | |
| tulanthoar | 1:59e0d83a4014 | 27 | DigitalIn swtch(PD_14); |
| tulanthoar | 1:59e0d83a4014 | 28 | |
| tulanthoar | 0:5d1a93a692a1 | 29 | int main() |
| tulanthoar | 0:5d1a93a692a1 | 30 | { |
| tulanthoar | 0:5d1a93a692a1 | 31 | printf("Basic HTTP server example\n"); |
| tulanthoar | 0:5d1a93a692a1 | 32 | |
| tulanthoar | 0:5d1a93a692a1 | 33 | EthernetInterface eth; |
| tulanthoar | 0:5d1a93a692a1 | 34 | eth.connect(); |
| tulanthoar | 0:5d1a93a692a1 | 35 | |
| tulanthoar | 0:5d1a93a692a1 | 36 | printf("The target IP address is '%s'\n", eth.get_ip_address()); |
| tulanthoar | 0:5d1a93a692a1 | 37 | |
| tulanthoar | 0:5d1a93a692a1 | 38 | TCPServer srv; |
| tulanthoar | 1:59e0d83a4014 | 39 | SocketAddress clt_addr; |
| tulanthoar | 0:5d1a93a692a1 | 40 | TCPSocket clt_sock; |
| tulanthoar | 0:5d1a93a692a1 | 41 | |
| tulanthoar | 0:5d1a93a692a1 | 42 | /* Open the server on ethernet stack */ |
| tulanthoar | 0:5d1a93a692a1 | 43 | srv.open(ð); |
| tulanthoar | 0:5d1a93a692a1 | 44 | |
| tulanthoar | 0:5d1a93a692a1 | 45 | /* Bind the HTTP port (TCP 80) to the server */ |
| tulanthoar | 0:5d1a93a692a1 | 46 | srv.bind(eth.get_ip_address(), 80); |
| tulanthoar | 0:5d1a93a692a1 | 47 | |
| tulanthoar | 0:5d1a93a692a1 | 48 | /* Can handle 5 simultaneous connections */ |
| tulanthoar | 0:5d1a93a692a1 | 49 | srv.listen(5); |
| tulanthoar | 1:59e0d83a4014 | 50 | srv.accept(&clt_sock, &clt_addr); |
| tulanthoar | 0:5d1a93a692a1 | 51 | while (true) { |
| tulanthoar | 1:59e0d83a4014 | 52 | wait(1); |
| tulanthoar | 0:5d1a93a692a1 | 53 | printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
| tulanthoar | 1:59e0d83a4014 | 54 | wait(1); |
| tulanthoar | 0:5d1a93a692a1 | 55 | clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE)); |
| tulanthoar | 1:59e0d83a4014 | 56 | wait(1); |
| tulanthoar | 1:59e0d83a4014 | 57 | clt_sock.send("hi\r\n", strlen("hi\r\n")); |
| tulanthoar | 1:59e0d83a4014 | 58 | if( swtch == 0 ) clt_sock.send("hi\r\n", strlen("hi\r\n")); |
| tulanthoar | 1:59e0d83a4014 | 59 | printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
| tulanthoar | 0:5d1a93a692a1 | 60 | } |
| tulanthoar | 0:5d1a93a692a1 | 61 | } |