Fork of original ST example for testing

Committer:
JojoS
Date:
Wed Nov 16 12:29:53 2016 +0000
Revision:
2:d45fed6919ef
Parent:
1:ec61ea9f67de
client socket is not closed after sending HTTP response

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adustm 1:ec61ea9f67de 1 #if !FEATURE_LWIP
adustm 1:ec61ea9f67de 2 #error [NOT_SUPPORTED] LWIP 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;
adustm 1:ec61ea9f67de 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) {
adustm 1:ec61ea9f67de 50 srv.accept(&clt_sock, &clt_addr);
adustm 1:ec61ea9f67de 51 printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
group-ST 0:6e8275981824 52 clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE));
JojoS 2:d45fed6919ef 53 clt_sock.close();
group-ST 0:6e8275981824 54 }
group-ST 0:6e8275981824 55 }