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