Simple TCP/IP Server (static web page_

Committer:
noutram
Date:
Mon Nov 20 17:30:47 2017 +0000
Revision:
0:5552cdb52638
Child:
2:31688a1ac73a
Simple TCP/IP Server

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:5552cdb52638 1 #if !FEATURE_LWIP
noutram 0:5552cdb52638 2 #error [NOT_SUPPORTED] LWIP not supported for this target
noutram 0:5552cdb52638 3 #endif
noutram 0:5552cdb52638 4
noutram 0:5552cdb52638 5 #include "mbed.h"
noutram 0:5552cdb52638 6 #include "EthernetInterface.h"
noutram 0:5552cdb52638 7 #include "TCPServer.h"
noutram 0:5552cdb52638 8 #include "TCPSocket.h"
noutram 0:5552cdb52638 9
noutram 0:5552cdb52638 10 #define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
noutram 0:5552cdb52638 11 #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
noutram 0:5552cdb52638 12 #define HTTP_MESSAGE_BODY "" \
noutram 0:5552cdb52638 13 "<html>" "\r\n" \
noutram 0:5552cdb52638 14 " <body style=\"display:flex;text-align:center\">" "\r\n" \
noutram 0:5552cdb52638 15 " <div style=\"margin:auto\">" "\r\n" \
noutram 0:5552cdb52638 16 " <h1>Hello World</h1>" "\r\n" \
noutram 0:5552cdb52638 17 " <p>It works !</p>" "\r\n" \
noutram 0:5552cdb52638 18 " </div>" "\r\n" \
noutram 0:5552cdb52638 19 " </body>" "\r\n" \
noutram 0:5552cdb52638 20 "</html>"
noutram 0:5552cdb52638 21
noutram 0:5552cdb52638 22 #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \
noutram 0:5552cdb52638 23 HTTP_HEADER_FIELDS "\r\n" \
noutram 0:5552cdb52638 24 "\r\n" \
noutram 0:5552cdb52638 25 HTTP_MESSAGE_BODY "\r\n"
noutram 0:5552cdb52638 26
noutram 0:5552cdb52638 27 #define IP "10.0.0.10"
noutram 0:5552cdb52638 28 #define NETMASK "255.0.0.0"
noutram 0:5552cdb52638 29 #define GATEWAY "10.0.0.1"
noutram 0:5552cdb52638 30
noutram 0:5552cdb52638 31
noutram 0:5552cdb52638 32 int main()
noutram 0:5552cdb52638 33 {
noutram 0:5552cdb52638 34 printf("Basic HTTP server example\n");
noutram 0:5552cdb52638 35
noutram 0:5552cdb52638 36 //Configure an ethernet connection
noutram 0:5552cdb52638 37 EthernetInterface eth;
noutram 0:5552cdb52638 38 eth.set_network(IP, NETMASK, GATEWAY);
noutram 0:5552cdb52638 39 eth.connect();
noutram 0:5552cdb52638 40 printf("The target IP address is '%s'\n", eth.get_ip_address());
noutram 0:5552cdb52638 41
noutram 0:5552cdb52638 42 //Now setup a web server
noutram 0:5552cdb52638 43 TCPServer srv; //TCP/IP Server
noutram 0:5552cdb52638 44 TCPSocket clt_sock; //Socket for communication
noutram 0:5552cdb52638 45 SocketAddress clt_addr; //Address of incoming connection
noutram 0:5552cdb52638 46
noutram 0:5552cdb52638 47 /* Open the server on ethernet stack */
noutram 0:5552cdb52638 48 srv.open(&eth);
noutram 0:5552cdb52638 49
noutram 0:5552cdb52638 50 /* Bind the HTTP port (TCP 80) to the server */
noutram 0:5552cdb52638 51 srv.bind(eth.get_ip_address(), 80);
noutram 0:5552cdb52638 52
noutram 0:5552cdb52638 53 /* Can handle 5 simultaneous connections */
noutram 0:5552cdb52638 54 srv.listen(5);
noutram 0:5552cdb52638 55
noutram 0:5552cdb52638 56 while (true) {
noutram 0:5552cdb52638 57 //Block and wait on an incoming connection
noutram 0:5552cdb52638 58 srv.accept(&clt_sock, &clt_addr);
noutram 0:5552cdb52638 59 printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
noutram 0:5552cdb52638 60
noutram 0:5552cdb52638 61 //Send static HTML response
noutram 0:5552cdb52638 62 clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE));
noutram 0:5552cdb52638 63 }
noutram 0:5552cdb52638 64 }