How to develop a simple web server application

I’m trying to develop a simple web server application with Nucleo f411re and w5100 Ethernet shield(http://www.kynix.com/Detail/194062/W5100.html). My question is why the client.is_fin_received() function always returns 0. Why doesn’t my web page complete?

PS : I have no idea about http protocol and html. I think the CR and LF ( ‘\n’ ‘\r’) characters are wrong.

Here is my main.cpp file

#include "mbed.h"
#include "WIZnetInterface.h"

#define USE_W5100
#define ST_NUCLEO
#define MYPORT    80

const char * IP_Addr    = "192.168.2.141";
const char * IP_Subnet  = "255.255.255.0";
const char * IP_Gateway = "192.168.1.1";
unsigned char MAC_Addr[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

Serial pc(USBTX, USBRX);

SPI spi(PA_7,PA_6,PA_5);
WIZnetInterface ethernet(&spi,PB_6,PA_10);

char page[]={"\r\nHTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><h1>PAGE</h1></body></html>\r\n\r\n"};

int i=0;

int main() {

pc.baud(9600);
pc.printf("Start\r\n");
char buffer[300];

while(1)
{
int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);

if (!ret) {
    pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
    ret = ethernet.connect();
    if (!ret) {
        pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
                  ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
    } else {
        pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
        exit(0);
    }
} else {
    pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
    exit(0);
}

TCPSocketServer server;
server.bind(MYPORT);
server.listen();

while (1) {
    pc.printf("\nWait for new connection...\r\n");
    TCPSocketConnection client;
    server.accept(client);
    client.set_blocking(false, 0); // Timeout=0.
    pc.printf("Connection from: %s\r\n", client.get_address());
    while (client.is_connected() == true) {
        int n = client.receive(buffer, sizeof(buffer));

        client.send(page,sizeof(page));

        wait(1);

        n = client.receive(buffer, sizeof(buffer));
        for(i=0; i<sizeof(buffer); i++)
        {
            pc.printf("%c",buffer[i]);
            if(buffer[i]==' ')
                pc.printf("\r\n");
        }

        pc.printf("\nclient is fin received : %d\r\n",client.is_fin_received());
        if(client.is_fin_received())
            client.close();
    }
    pc.printf("Disconnected.\r\n");
    }
}
}

My guess is that rather than waiting for the client to download the data once, you are re-supplying it in every pass of the loop. You should only supply it once.

The other thing is that you should examine the request from the client to see what it wants. Wait for it to supply HTTP headers - you’re looking for Host and GET headers in the request.

As I understand you gonna connect from CPP in your computer to 000webhost website right?

I have no idea about CPP. However are you using sockets?