8 years, 5 months ago.

How to create Simple Web Server with Mbed and W5100 Ethernet Shield

Hi guys,

I'm trying to develop simple web server application with nucleo f411re and w5100 Ethernet shield. 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 code

#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[]={"HTTP/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");
        }
    }
}

Result ;

/media/uploads/FuzzyMind/screenshot_from_2015-11-18_11-41-38.png

Be the first to answer this question.