Estoy intentando desarrollar una aplicación de servidor web simple con Nucleo f411re y w5100 Ethernet Shield. Mi pregunta es por qué la función client.is_fin_received () siempre devuelve 0. ¿Por qué no se completa mi página web?
PS: No tengo idea sobre el protocolo http y html. Creo que los caracteres CR y LF ('\ n' '\ r') están mal.
Aquí está mi archivo main.cpp
#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");
}
}
}