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@3:f4f66901181c, 2017-08-08 (annotated)
- Committer:
- Nathan Yonkee
- Date:
- Tue Aug 08 23:56:11 2017 -0600
- Revision:
- 3:f4f66901181c
- Parent:
- 1:59e0d83a4014
- Child:
- 4:24963e7cc783
use fixed IP address
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tulanthoar | 0:5d1a93a692a1 | 1 | #if !FEATURE_LWIP |
Nathan Yonkee |
3:f4f66901181c | 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 | |
Nathan Yonkee |
3:f4f66901181c | 29 | int main() { |
tulanthoar | 0:5d1a93a692a1 | 30 | printf("Basic HTTP server example\n"); |
Nathan Yonkee |
3:f4f66901181c | 31 | |
tulanthoar | 0:5d1a93a692a1 | 32 | EthernetInterface eth; |
Nathan Yonkee |
3:f4f66901181c | 33 | eth.set_network("192.168.1.101", "255.255.255.0","192.168.1.1"); |
tulanthoar | 0:5d1a93a692a1 | 34 | eth.connect(); |
Nathan Yonkee |
3:f4f66901181c | 35 | |
tulanthoar | 0:5d1a93a692a1 | 36 | printf("The target IP address is '%s'\n", eth.get_ip_address()); |
Nathan Yonkee |
3:f4f66901181c | 37 | printf("The target netmask is '%s'\n", eth.get_netmask()); |
Nathan Yonkee |
3:f4f66901181c | 38 | printf("The target gateway is '%s'\n", eth.get_gateway()); |
Nathan Yonkee |
3:f4f66901181c | 39 | |
tulanthoar | 0:5d1a93a692a1 | 40 | TCPServer srv; |
tulanthoar | 1:59e0d83a4014 | 41 | SocketAddress clt_addr; |
tulanthoar | 0:5d1a93a692a1 | 42 | TCPSocket clt_sock; |
Nathan Yonkee |
3:f4f66901181c | 43 | |
tulanthoar | 0:5d1a93a692a1 | 44 | /* Open the server on ethernet stack */ |
tulanthoar | 0:5d1a93a692a1 | 45 | srv.open(ð); |
Nathan Yonkee |
3:f4f66901181c | 46 | |
tulanthoar | 0:5d1a93a692a1 | 47 | /* Bind the HTTP port (TCP 80) to the server */ |
tulanthoar | 0:5d1a93a692a1 | 48 | srv.bind(eth.get_ip_address(), 80); |
Nathan Yonkee |
3:f4f66901181c | 49 | |
tulanthoar | 0:5d1a93a692a1 | 50 | /* Can handle 5 simultaneous connections */ |
tulanthoar | 0:5d1a93a692a1 | 51 | srv.listen(5); |
tulanthoar | 1:59e0d83a4014 | 52 | srv.accept(&clt_sock, &clt_addr); |
tulanthoar | 0:5d1a93a692a1 | 53 | while (true) { |
tulanthoar | 1:59e0d83a4014 | 54 | wait(1); |
tulanthoar | 0:5d1a93a692a1 | 55 | printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
tulanthoar | 1:59e0d83a4014 | 56 | wait(1); |
tulanthoar | 0:5d1a93a692a1 | 57 | clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE)); |
tulanthoar | 1:59e0d83a4014 | 58 | wait(1); |
Nathan Yonkee |
3:f4f66901181c | 59 | clt_sock.send("hi\r\n", strlen("hi\r\n")); |
tulanthoar | 1:59e0d83a4014 | 60 | if( swtch == 0 ) clt_sock.send("hi\r\n", strlen("hi\r\n")); |
tulanthoar | 1:59e0d83a4014 | 61 | printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
tulanthoar | 0:5d1a93a692a1 | 62 | } |
tulanthoar | 0:5d1a93a692a1 | 63 | } |