2
\$\begingroup\$

I'm trying to develop a 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 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");
    }
}
}

Result: ScreenShot

\$\endgroup\$
1
\$\begingroup\$

I'm not familiar with that platform but two possibilities I can see are:

  • I don't think you need the extra CR/LF at the start of your response (but probably not the main issue).

  • You're not including Content-Length in the header so I think the client will be waiting for the socket to be closed at your end which is why is_fin_received() never returns true.

So I'd try including the Content-Length header which is the length of the of the main body. I'm fairly sure it needs to be the length including the CR/LF characters but you should get an HTTP error response if it's wrong and can adjust.

\$\endgroup\$
  • \$\begingroup\$ I edited page[] array as char page[]={"\r\nHTTP/1.0 200 OK\r\nContent-Type: text/html\r\nContent-Length: 39\r\n\r\n<html><body><h1>PAGE</h1></body></html>\r\n\r\n"}; and problem solved .I really appreciate your help thank you so much. \$\endgroup\$ – Fuzzy_Mind Nov 21 '15 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.