Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: scooter_mbed_correction_mbed_os
Fork of html by
Diff: html.cpp
- Branch:
- html_mbed_os
- Revision:
- 2:468edcd380ae
- Parent:
- 1:ba608856b208
diff -r ba608856b208 -r 468edcd380ae html.cpp
--- a/html.cpp Wed Aug 12 15:53:49 2015 +0000
+++ b/html.cpp Sat Nov 26 16:05:42 2016 +0000
@@ -102,16 +102,16 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "EthernetInterface.h"
#include <stdlib.h>
#include <string.h>
+#include "TCPSocket.h"
+#include "TCPServer.h"
+#include "EthernetInterface.h"
//********************
//* specified includes
#include "html.h"
-#include "rtos.h"
-
#define _HTML_DEBUG
#define _IP_DEBUG
@@ -123,11 +123,12 @@
EthernetInterface eth; // ethernet layer management object
-TCPSocketServer svr; //
+TCPServer svr; //
bool serverIsListened = false;// flag to indicate server is listening
#define PORT 80 // tcp port for socket listeningt
-TCPSocketConnection client;// socket to identify client side
-bool clientIsConnected = false;// flag that indicate if a web client is avalaible
+TCPSocket clt_sock;// socket to identify clt_sock side
+SocketAddress clt_addr; //address clt_sock side
+bool clt_sockIsConnected = false;// flag that indicate if a web clt_sock is avalaible
char *html_page; // pointer to memory area of Html Page
@@ -155,6 +156,7 @@
printf("\r\nError Open File: -%s-", Path);
return(NULL);
}
+ else
#ifdef _HTML_DEBUG
printf("\r\nOpen File.. \"%s\"", Path);
@@ -324,15 +326,16 @@
{pfPtr=fPtr;// affectation du pointeur public
//setup ethernet interface
- eth.init(); //Use DHCP
+ // eth.init(); //Use DHCP
eth.connect();
#ifdef _IP_DEBUG
- printf("IP Address is %s\n\r", eth.getIPAddress());// display server ip address
+ printf("IP Address is %s\n\r", eth.get_ip_address());// display server ip address
#endif
- // Set the callbacks for Listening
- //srv.setOnEvent(&onListeningTCPSocketEvent); // create a call back fonction associated with listning socket
- //setup srv tcp socket
- if(svr.bind(PORT)< 0) {
+ /* Open the server on ethernet stack */
+ svr.open(ð);
+
+ /* Bind the HTTP port (TCP 80) to the server */
+ if(svr.bind(PORT)< 0) {
#ifdef _IP_DEBUG
printf("tcp server bind failed.\n\r");
#endif
@@ -343,7 +346,7 @@
#endif
serverIsListened = true;
}
-
+ //accept only 1 connection
if(svr.listen(1) < 0) {
#ifdef _IP_DEBUG
printf("tcp server listen failed.\n\r");
@@ -364,7 +367,7 @@
int DeInit_Web_Server(void)
{int iError;
serverIsListened=false;//
-iError=svr.close (true);// close main server socket
+iError=svr.close ();// close main server socket
return iError;
}
@@ -385,35 +388,35 @@
while ( serverIsListened) {
//blocking mode(never timeout)
- if(svr.accept(client)<0) {
+ if(svr.accept(&clt_sock, &clt_addr)<0) {
#ifdef _TCP_DEBUG
printf("failed to accept connection.\n\r");
#endif
} else {
#ifdef _TCP_DEBUG
- printf("connection success!\n\rIP: %s\n\r",client.get_address());
+ printf("accept, connection success! IP:%s PORT:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
#endif
- clientIsConnected = true;
+ clt_sockIsConnected = true;
// Setup the new socket events
- // client.setOnEvent(&onConnectedTCPSocketEvent);
+ // clt_sock.setOnEvent(&onConnectedTCPSocketEvent);
// We can find out from where the connection is coming by looking at the
// Host parameter of the accept() method
- while(clientIsConnected) {
+ while(clt_sockIsConnected) {
char buffer[1024] = {};
- switch(client.receive(buffer, 1023)) {
+ switch(clt_sock.recv(buffer, 1023)) {
case 0:
#ifdef _TCP_DEBUG
printf("recieved buffer is empty.\n\r");
#endif
- clientIsConnected = false;
+ clt_sockIsConnected = false;
break;
case -1:
#ifdef _TCP_DEBUG
- printf("failed to read data from client.\n\r");
+ printf("failed to read data from clt_sock.\n\r");
#endif
- clientIsConnected = false;
+ clt_sockIsConnected = false;
break;
default:
#ifdef _HTTP_DEBUG
@@ -452,19 +455,19 @@
char echoHeader[256] = {};
sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r",strlen(html_page));
- client.send(echoHeader,strlen(echoHeader));
+ clt_sock.send(echoHeader,strlen(echoHeader));
#ifdef _HTTP_DEBUG
printf("%s",echoHeader);// debut http header sent
#endif
- //client.send(buffer,strlen(buffer));// realise echo request to client
+ //clt_sock.send(buffer,strlen(buffer));// realise echo request to clt_sock
- client.send(html_page,strlen(html_page));// realise echo request to client
+ clt_sock.send(html_page,strlen(html_page));// realise echo request to clt_sock
#ifdef _HTTP_DEBUG
printf("%s",html_page);
#endif
- clientIsConnected = false;// close connection with this client at end of http response
+ clt_sockIsConnected = false;// close connection with this clt_sock at end of http response
#ifdef _HTTP_DEBUG
printf("web page sent.\n\r");
#endif
@@ -472,11 +475,11 @@
break;
}// end case
//Thread::wait(10);// sleep for 10 ms
- }// end while Client is Connected
+ }// end while clt_sock is Connected
#ifdef _HTTP_DEBUG
printf("close connection.\n\rtcp server is listening...\n\r");
#endif
- client.close();
+ clt_sock.close();
}
