for HTTP-3A

Dependencies:   mbed TextLCD

Committer:
group-ST
Date:
Thu Sep 22 11:20:29 2016 +0000
Revision:
0:6e8275981824
Child:
1:ec61ea9f67de
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-ST 0:6e8275981824 1 #if !FEATURE_IPV4
group-ST 0:6e8275981824 2 #error [NOT_SUPPORTED] IPV4 not supported for this target
group-ST 0:6e8275981824 3 #endif
group-ST 0:6e8275981824 4
group-ST 0:6e8275981824 5 #include "mbed.h"
group-ST 0:6e8275981824 6 #include "EthernetInterface.h"
group-ST 0:6e8275981824 7 #include "TCPServer.h"
group-ST 0:6e8275981824 8 #include "TCPSocket.h"
group-ST 0:6e8275981824 9
group-ST 0:6e8275981824 10 #define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
group-ST 0:6e8275981824 11 #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
group-ST 0:6e8275981824 12 #define HTTP_MESSAGE_BODY "" \
group-ST 0:6e8275981824 13 "<html>" "\r\n" \
group-ST 0:6e8275981824 14 " <body style=\"display:flex;text-align:center\">" "\r\n" \
group-ST 0:6e8275981824 15 " <div style=\"margin:auto\">" "\r\n" \
group-ST 0:6e8275981824 16 " <h1>Hello World</h1>" "\r\n" \
group-ST 0:6e8275981824 17 " <p>It works !</p>" "\r\n" \
group-ST 0:6e8275981824 18 " </div>" "\r\n" \
group-ST 0:6e8275981824 19 " </body>" "\r\n" \
group-ST 0:6e8275981824 20 "</html>"
group-ST 0:6e8275981824 21
group-ST 0:6e8275981824 22 #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \
group-ST 0:6e8275981824 23 HTTP_HEADER_FIELDS "\r\n" \
group-ST 0:6e8275981824 24 "\r\n" \
group-ST 0:6e8275981824 25 HTTP_MESSAGE_BODY "\r\n"
group-ST 0:6e8275981824 26
group-ST 0:6e8275981824 27 int main()
group-ST 0:6e8275981824 28 {
group-ST 0:6e8275981824 29 printf("Basic HTTP server example\n");
group-ST 0:6e8275981824 30
group-ST 0:6e8275981824 31 EthernetInterface eth;
group-ST 0:6e8275981824 32 eth.connect();
group-ST 0:6e8275981824 33
group-ST 0:6e8275981824 34 printf("The target IP address is '%s'\n", eth.get_ip_address());
group-ST 0:6e8275981824 35
group-ST 0:6e8275981824 36 TCPServer srv;
group-ST 0:6e8275981824 37 TCPSocket clt_sock;
group-ST 0:6e8275981824 38 //SocketAddress clt_addr;
group-ST 0:6e8275981824 39
group-ST 0:6e8275981824 40 /* Open the server on ethernet stack */
group-ST 0:6e8275981824 41 srv.open(&eth);
group-ST 0:6e8275981824 42
group-ST 0:6e8275981824 43 /* Bind the HTTP port (TCP 80) to the server */
group-ST 0:6e8275981824 44 srv.bind(eth.get_ip_address(), 80);
group-ST 0:6e8275981824 45
group-ST 0:6e8275981824 46 /* Can handle 5 simultaneous connections */
group-ST 0:6e8275981824 47 srv.listen(5);
group-ST 0:6e8275981824 48
group-ST 0:6e8275981824 49 while (true) {
group-ST 0:6e8275981824 50 // Will be possible in releaseb 126
group-ST 0:6e8275981824 51 //srv.accept(&clt_sock, &clt_addr);
group-ST 0:6e8275981824 52 //printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
group-ST 0:6e8275981824 53 srv.accept(&clt_sock);
group-ST 0:6e8275981824 54 printf("new client accepted\n");
group-ST 0:6e8275981824 55 clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE));
group-ST 0:6e8275981824 56 }
group-ST 0:6e8275981824 57 }