Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4547

General • Pico W : modify example tcpserver in web server

$
0
0
Hello.
I wanted to adapt the example of the pico W tcp server to transform it into a web server.
I modified the port by putting port 80, replace the sending of data with the sending of a header
html and a form.
After a few small adaptations, the form is displayed correctly in the browser but when you enter a zone to return the content of the form everything stops with the message "test failed".
In fact the program stops because the pbuf pointer of the tcp_server_recv function is null.
I have the impression that the tcp driver does not transmit the response from the form to the function.
I must be missing a step, or there is a desynchronization between the browser and the server.
Could anyone give me a line of research?
Thanck


This is a one modify function.

Code:

err_t tcp_server_send_data(void *arg, struct tcp_pcb *tpcb){ char * ligne1= " <!DOCTYPE html> <html> <body>Bienvenue sur le Pico W. <br>   <form action=\"./commande\">"; char * ligne2= " <br> Commande : <br><input type=\"Commande\" id=\"commande\" name=\"com\" /> <br> "; char * ligne3=" </form><br><p>LED is {state}</p><br><p>{message}</p><br><form action=\"./stop\"><input type=\"submit\" value=\"STOP\" /></form></body></html>\r\n\r\n";        char * entete="HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n";    int tailleent=strlen(entete);    TCP_SERVER_T *state = (TCP_SERVER_T*)arg;           for(int i=0; i< tailleent; i++) {        state->buffer_sent[i] = entete[i];    }    DEBUG_printf("message = %s\n",state->buffer_sent);    state->sent_len = 0;    DEBUG_printf("Writing %ld bytes to client\n", BUF_SIZE);    // this method is callback from lwIP, so cyw43_arch_lwip_begin is not required, however you    // can use this method to cause an assertion in debug mode, if this method is called when    // cyw43_arch_lwip_begin IS needed    cyw43_arch_lwip_check();    err_t err = tcp_write(tpcb, state->buffer_sent, tailleent, TCP_WRITE_FLAG_COPY);    if (err != ERR_OK) {        DEBUG_printf("Failed to write data %d\n", err);        return tcp_server_result(arg, -1);    }    sleep_ms(1000);    //*state = arg;    int taille1=strlen(ligne1);    for(int i=0; i< taille1; i++) {        state->buffer_sent[i] = ligne1[i];    }    int taille2=strlen(ligne2);    for(int i=0; i< taille2; i++) {        state->buffer_sent[taille1+i] = ligne2[i];    }    int taille3=strlen(ligne3);    for(int i=0; i< taille3; i++) {        state->buffer_sent[taille1+taille2+i] = ligne3[i];    }    int taille=taille1+taille2+taille3;    DEBUG_printf("Taille message= %ld bytes \n", taille);    DEBUG_printf("message = %s\n",state->buffer_sent);    state->sent_len = 0;    DEBUG_printf("Writing %ld bytes to client\n", BUF_SIZE);    // this method is callback from lwIP, so cyw43_arch_lwip_begin is not required, however you    // can use this method to cause an assertion in debug mode, if this method is called when    // cyw43_arch_lwip_begin IS needed    cyw43_arch_lwip_check();    err = tcp_write(tpcb, state->buffer_sent, taille, TCP_WRITE_FLAG_COPY);    if (err != ERR_OK) {        DEBUG_printf("Failed to write data %d\n", err);        return tcp_server_result(arg, -1);    }    sleep_ms(5000);    return ERR_OK;}

Statistics: Posted by VincentARM — Sun Feb 11, 2024 9:04 pm — Replies 0 — Views 23



Viewing all articles
Browse latest Browse all 4547

Trending Articles