
Simple TCP/IP Server (static web page_
main.cpp@2:31688a1ac73a, 2019-11-08 (annotated)
- Committer:
- noutram
- Date:
- Fri Nov 08 14:19:22 2019 +0000
- Revision:
- 2:31688a1ac73a
- Parent:
- 0:5552cdb52638
- Child:
- 3:c62ab6a06986
Updated for 2019
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:5552cdb52638 | 1 | #include "mbed.h" |
noutram | 0:5552cdb52638 | 2 | #include "EthernetInterface.h" |
noutram | 0:5552cdb52638 | 3 | #include "TCPServer.h" |
noutram | 0:5552cdb52638 | 4 | #include "TCPSocket.h" |
noutram | 0:5552cdb52638 | 5 | |
noutram | 0:5552cdb52638 | 6 | #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" |
noutram | 0:5552cdb52638 | 7 | #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" |
noutram | 0:5552cdb52638 | 8 | #define HTTP_MESSAGE_BODY "" \ |
noutram | 0:5552cdb52638 | 9 | "<html>" "\r\n" \ |
noutram | 0:5552cdb52638 | 10 | " <body style=\"display:flex;text-align:center\">" "\r\n" \ |
noutram | 0:5552cdb52638 | 11 | " <div style=\"margin:auto\">" "\r\n" \ |
noutram | 0:5552cdb52638 | 12 | " <h1>Hello World</h1>" "\r\n" \ |
noutram | 0:5552cdb52638 | 13 | " <p>It works !</p>" "\r\n" \ |
noutram | 0:5552cdb52638 | 14 | " </div>" "\r\n" \ |
noutram | 0:5552cdb52638 | 15 | " </body>" "\r\n" \ |
noutram | 0:5552cdb52638 | 16 | "</html>" |
noutram | 0:5552cdb52638 | 17 | |
noutram | 0:5552cdb52638 | 18 | #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ |
noutram | 0:5552cdb52638 | 19 | HTTP_HEADER_FIELDS "\r\n" \ |
noutram | 0:5552cdb52638 | 20 | "\r\n" \ |
noutram | 0:5552cdb52638 | 21 | HTTP_MESSAGE_BODY "\r\n" |
noutram | 0:5552cdb52638 | 22 | |
noutram | 0:5552cdb52638 | 23 | #define IP "10.0.0.10" |
noutram | 0:5552cdb52638 | 24 | #define NETMASK "255.0.0.0" |
noutram | 0:5552cdb52638 | 25 | #define GATEWAY "10.0.0.1" |
noutram | 0:5552cdb52638 | 26 | |
noutram | 0:5552cdb52638 | 27 | |
noutram | 0:5552cdb52638 | 28 | int main() |
noutram | 0:5552cdb52638 | 29 | { |
noutram | 0:5552cdb52638 | 30 | printf("Basic HTTP server example\n"); |
noutram | 0:5552cdb52638 | 31 | |
noutram | 0:5552cdb52638 | 32 | //Configure an ethernet connection |
noutram | 0:5552cdb52638 | 33 | EthernetInterface eth; |
noutram | 0:5552cdb52638 | 34 | eth.set_network(IP, NETMASK, GATEWAY); |
noutram | 0:5552cdb52638 | 35 | eth.connect(); |
noutram | 0:5552cdb52638 | 36 | printf("The target IP address is '%s'\n", eth.get_ip_address()); |
noutram | 0:5552cdb52638 | 37 | |
noutram | 0:5552cdb52638 | 38 | //Now setup a web server |
noutram | 2:31688a1ac73a | 39 | TCPSocket srv; //TCP/IP Server |
noutram | 0:5552cdb52638 | 40 | TCPSocket clt_sock; //Socket for communication |
noutram | 0:5552cdb52638 | 41 | SocketAddress clt_addr; //Address of incoming connection |
noutram | 0:5552cdb52638 | 42 | |
noutram | 0:5552cdb52638 | 43 | /* Open the server on ethernet stack */ |
noutram | 0:5552cdb52638 | 44 | srv.open(ð); |
noutram | 0:5552cdb52638 | 45 | |
noutram | 0:5552cdb52638 | 46 | /* Bind the HTTP port (TCP 80) to the server */ |
noutram | 0:5552cdb52638 | 47 | srv.bind(eth.get_ip_address(), 80); |
noutram | 0:5552cdb52638 | 48 | |
noutram | 0:5552cdb52638 | 49 | /* Can handle 5 simultaneous connections */ |
noutram | 0:5552cdb52638 | 50 | srv.listen(5); |
noutram | 0:5552cdb52638 | 51 | |
noutram | 0:5552cdb52638 | 52 | while (true) { |
noutram | 0:5552cdb52638 | 53 | //Block and wait on an incoming connection |
noutram | 2:31688a1ac73a | 54 | srv.accept(); |
noutram | 0:5552cdb52638 | 55 | printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
noutram | 0:5552cdb52638 | 56 | |
noutram | 0:5552cdb52638 | 57 | //Send static HTML response |
noutram | 0:5552cdb52638 | 58 | clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE)); |
noutram | 0:5552cdb52638 | 59 | } |
noutram | 0:5552cdb52638 | 60 | } |