ip - How can I send a simple HTTP request with a lwIP stack? -
please move/close if question isn't relevant.
core: cortex-m4
microprocessor: ti tm4c1294ncpdt.
ip stack: lwip 1.4.1
i using microprocessor info logging, , want send info separate web server via http request in form of:
http://123.456.789.012:8800/process.php?data1=foo&data2=bar&time=1234568789
and want processor able see response header (i.e if 200 ok or went wrong) - not have display/recieve actual content.
lwip has http server microprocessor, i'm after opposite (microprocessor client).
i not sure how packets correlate request/response headers, i'm not sure how i'm meant send/recieve information.
this ended beingness pretty simple implement, forgot update question.
i pretty much followed instructions given on this site, raw/tcp 'documentation'.
basically, http request encoded in tcp packets, send info php server, sent http request using tcp packets (lwip work).
the http packet want send looks this:
head /process.php?data1=12&data2=5 http/1.0
host: mywebsite.com
to "translate" text understood http server, have add together "\r\n" carriage return/newline in code. looks this:
char *string = "head /process.php?data1=12&data2=5 http/1.0\r\nhost: mywebsite.com\r\n\r\n ";
note end has 2 lots of "\r\n"
you can utilize or head, because didn't care html site php server returned, used head (it returns 200 ok on success, or different code on failure).
the lwip raw/tcp works on callbacks. set callback functions, force info want tcp buffer (in case, tcp string specified above), , tell lwip send packet.
function set tcp connection (this function straight called application every time want send tcp packet):
void tcp_setup(void) { uint32_t info = 0xdeadbeef; /* create ip */ struct ip_addr ip; ip4_addr(&ip, 110,777,888,999); //ip of php server /* create command block */ testpcb = tcp_new(); //testpcb global struct tcp_pcb // defined lwip /* dummy info pass callbacks*/ tcp_arg(testpcb, &data); /* register callbacks pcb */ tcp_err(testpcb, tcperrorhandler); tcp_recv(testpcb, tcprecvcallback); tcp_sent(testpcb, tcpsendcallback); /* connect */ tcp_connect(testpcb, &ip, 80, connectcallback); }
once connection php server established, 'connectcallback' function called lwip:
/* connection established callback, err unused , homecoming 0 */ err_t connectcallback(void *arg, struct tcp_pcb *tpcb, err_t err) { uartprintf("connection established.\n"); uartprintf("now sending packet\n"); tcp_send_packet(); homecoming 0; }
this function calls actual function tcp_send_packet() sends http request, follows:
uint32_t tcp_send_packet(void) { char *string = "head /process.php?data1=12&data2=5 http/1.0\r\nhost: mywebsite.com\r\n\r\n "; uint32_t len = strlen(string); /* force buffer */ error = tcp_write(testpcb, string, strlen(string), tcp_write_flag_copy); if (error) { uartprintf("error: code: %d (tcp_send_packet :: tcp_write)\n", error); homecoming 1; } /* send */ error = tcp_output(testpcb); if (error) { uartprintf("error: code: %d (tcp_send_packet :: tcp_output)\n", error); homecoming 1; } homecoming 0; }
once tcp packet has been sent (this need if want "hope best" , don't care if info sent), php server homecoming tcp packet (with 200 ok, etc. , html code if used instead of head). code can read , verified in next code:
err_t tcprecvcallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { uartprintf("data recieved.\n"); if (p == null) { uartprintf("the remote host closed connection.\n"); uartprintf("now i'm closing connection.\n"); tcp_close_con(); homecoming err_abrt; } else { uartprintf("number of pbufs %d\n", pbuf_clen(p)); uartprintf("contents of pbuf %s\n", (char *)p->payload); } homecoming 0; }
p->payload contains actual "200 ok", etc. information. helps someone.
i have left out error checking in code above simplify answer.
http ip embedded packet lwip