Simple TCP/IP Server (static web page_
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /*#if !FEATURE_LWIP 00002 #error [NOT_SUPPORTED] LWIP not supported for this target 00003 #endif 00004 */ 00005 #include "mbed.h" 00006 #include "EthernetInterface.h" 00007 #include "TCPSocket.h" 00008 00009 #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" 00010 #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" 00011 #define HTTP_MESSAGE_BODY "" \ 00012 "<html>" "\r\n" \ 00013 " <body style=\"display:flex;text-align:center\">" "\r\n" \ 00014 " <div style=\"margin:auto\">" "\r\n" \ 00015 " <h1>Hello World</h1>" "\r\n" \ 00016 " <p>It works !</p>" "\r\n" \ 00017 " </div>" "\r\n" \ 00018 " </body>" "\r\n" \ 00019 "</html>" "\r\n" 00020 00021 #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ 00022 HTTP_HEADER_FIELDS "\r\n" \ 00023 "\r\n" \ 00024 HTTP_MESSAGE_BODY "\r\n" 00025 #define HTTP_TITLE "<head><title> Plymouth Uni Weather Page </title></head>" "\r\n" 00026 #define HTTP_FORMAT_1 "<body style=\"display:flex;text-align:center\">" "\r\n" \ 00027 "<div style=\"margin:auto\">" "\r\n" 00028 #define HTTP_BOTTOM "</html>" "\r\n" 00029 00030 #define IP "10.0.0.10" 00031 #define NETMASK "255.255.255.0" 00032 #define GATEWAY "10.0.0.1" 00033 00034 00035 //const char *const myHTTP = HTTP_RESPONSE; //This if you wish to set above Compiler defines into Flash Silicon 00036 00037 void ReadFlag_ISR(void); 00038 bool ReadFlag=true; 00039 00040 int main() 00041 { 00042 printf("\r\nBasic HTTP server example\r\n"); 00043 00044 //generic error code handle 00045 int err=0; 00046 00047 // Ethernet Interface 00048 EthernetInterface eth; 00049 eth.set_network(IP, NETMASK, GATEWAY); // comment this line out if you wish to use DHCP 00050 eth.connect(); 00051 printf("\r\nThe target IP address is '%s'\r\n", eth.get_ip_address()); 00052 00053 // Use to be :- TCPServer srv;!!! 00054 TCPSocket srv; 00055 TCPSocket *clt_sock; 00056 00057 // Open the server on ethernet stack 00058 srv.open(ð); 00059 00060 // BIND the server to the HTTP port (TCP 80) 00061 err=srv.bind(eth.get_ip_address(), 80); 00062 00063 printf("IP Address confirm at : %s\n\r",eth.get_ip_address()); 00064 00065 if(err==0) { 00066 printf("Bind OK\n\r"); 00067 } else { 00068 printf("Bind error=%d\n\r",err); 00069 } 00070 00071 // LISTEN Can handle 5 simultaneous connections 00072 err=srv.listen(5); 00073 if(err==0) { 00074 printf("Listening OK\n\r"); 00075 } else { 00076 printf("Listen error=%d\n\r",err); 00077 } 00078 00079 while (true) { 00080 // ACCEPT Accepting connections now..... 00081 clt_sock=srv.accept(); 00082 00083 printf("Waiting.....\n\r"); 00084 00085 // the rest of this line to use Flash Silicon *see notes above line number 35" myHTTP,strlen(myHTTP)); 00086 printf("%s STRING LENGTH is:%d\n\r",HTTP_RESPONSE,strlen(HTTP_RESPONSE)); 00087 00088 //myHTTP,mydatasize)the rest of this line to use Flash Silicon *see notes above line number 35" myHTTP,strlen(myHTTP)); 00089 clt_sock->send(HTTP_RESPONSE, strlen(HTTP_RESPONSE)); 00090 if(err==0) { 00091 printf("Sent OK!\n\r"); 00092 } else { 00093 printf("Send error=%d\n\r",err); 00094 } 00095 00096 clt_sock->close(); 00097 wait_us(1000000);//Delay 1 second 00098 } 00099 }
Generated on Sat Jul 23 2022 07:32:45 by
1.7.2