rh li
/
mbed-os-f767ZI-test
init publish
main.cpp@0:72f7aceee9cd, 2017-02-03 (annotated)
- Committer:
- lrh
- Date:
- Fri Feb 03 07:28:31 2017 +0000
- Revision:
- 0:72f7aceee9cd
init the program of tcp server
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lrh | 0:72f7aceee9cd | 1 | #if !FEATURE_LWIP |
lrh | 0:72f7aceee9cd | 2 | #error [NOT_SUPPORTED] LWIP not supported for this target |
lrh | 0:72f7aceee9cd | 3 | #endif |
lrh | 0:72f7aceee9cd | 4 | |
lrh | 0:72f7aceee9cd | 5 | #include "mbed.h" |
lrh | 0:72f7aceee9cd | 6 | #include "EthernetInterface.h" |
lrh | 0:72f7aceee9cd | 7 | #include "TCPServer.h" |
lrh | 0:72f7aceee9cd | 8 | #include "TCPSocket.h" |
lrh | 0:72f7aceee9cd | 9 | |
lrh | 0:72f7aceee9cd | 10 | #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" |
lrh | 0:72f7aceee9cd | 11 | #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" |
lrh | 0:72f7aceee9cd | 12 | #define HTTP_MESSAGE_BODY "" \ |
lrh | 0:72f7aceee9cd | 13 | "<html>" "\r\n" \ |
lrh | 0:72f7aceee9cd | 14 | " <body style=\"display:flex;text-align:center\">" "\r\n" \ |
lrh | 0:72f7aceee9cd | 15 | " <div style=\"margin:auto\">" "\r\n" \ |
lrh | 0:72f7aceee9cd | 16 | " <h1>Hello World</h1>" "\r\n" \ |
lrh | 0:72f7aceee9cd | 17 | " <p>It works !</p>" "\r\n" \ |
lrh | 0:72f7aceee9cd | 18 | " </div>" "\r\n" \ |
lrh | 0:72f7aceee9cd | 19 | " </body>" "\r\n" \ |
lrh | 0:72f7aceee9cd | 20 | "</html>" |
lrh | 0:72f7aceee9cd | 21 | |
lrh | 0:72f7aceee9cd | 22 | #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ |
lrh | 0:72f7aceee9cd | 23 | HTTP_HEADER_FIELDS "\r\n" \ |
lrh | 0:72f7aceee9cd | 24 | "\r\n" \ |
lrh | 0:72f7aceee9cd | 25 | HTTP_MESSAGE_BODY "\r\n" |
lrh | 0:72f7aceee9cd | 26 | |
lrh | 0:72f7aceee9cd | 27 | int main() |
lrh | 0:72f7aceee9cd | 28 | { |
lrh | 0:72f7aceee9cd | 29 | printf("Basic HTTP server example\n"); |
lrh | 0:72f7aceee9cd | 30 | |
lrh | 0:72f7aceee9cd | 31 | EthernetInterface eth; |
lrh | 0:72f7aceee9cd | 32 | eth.connect(); |
lrh | 0:72f7aceee9cd | 33 | |
lrh | 0:72f7aceee9cd | 34 | printf("The target IP address is '%s'\n", eth.get_ip_address()); |
lrh | 0:72f7aceee9cd | 35 | |
lrh | 0:72f7aceee9cd | 36 | TCPServer srv; |
lrh | 0:72f7aceee9cd | 37 | TCPSocket clt_sock; |
lrh | 0:72f7aceee9cd | 38 | SocketAddress clt_addr; |
lrh | 0:72f7aceee9cd | 39 | |
lrh | 0:72f7aceee9cd | 40 | /* Open the server on ethernet stack */ |
lrh | 0:72f7aceee9cd | 41 | srv.open(ð); |
lrh | 0:72f7aceee9cd | 42 | |
lrh | 0:72f7aceee9cd | 43 | /* Bind the HTTP port (TCP 80) to the server */ |
lrh | 0:72f7aceee9cd | 44 | srv.bind(eth.get_ip_address(), 80); |
lrh | 0:72f7aceee9cd | 45 | |
lrh | 0:72f7aceee9cd | 46 | /* Can handle 5 simultaneous connections */ |
lrh | 0:72f7aceee9cd | 47 | srv.listen(5); |
lrh | 0:72f7aceee9cd | 48 | |
lrh | 0:72f7aceee9cd | 49 | while (true) { |
lrh | 0:72f7aceee9cd | 50 | srv.accept(&clt_sock, &clt_addr); |
lrh | 0:72f7aceee9cd | 51 | printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
lrh | 0:72f7aceee9cd | 52 | clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE)); |
lrh | 0:72f7aceee9cd | 53 | } |
lrh | 0:72f7aceee9cd | 54 | } |