lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-users] httpd example - code suggestions needed


From: Bahadir Balban
Subject: [lwip-users] httpd example - code suggestions needed
Date: Wed, 12 Mar 2003 01:04:13 +0000

Hi there,

I'm a student using lwip in part of my project. For quick demonstration 
purposes, and just to simply add a few things for myself, I am using the very 
simple piece of httpd code that uses lwip's own api, included in the 
documentation.

The code's written with the usual bind, listen, accept, process, delete 
sequence.

It works fine for the first 2 HTTP GET requests, but then it fails in later 
subsequent connections( 3rd, 4th ...) because when

  inbuf = netconn_recv(conn);

is called, inbuf simply has trash data, instead of a GET request.
I've thought about the fragmented buffers but when I used something like:

 do {
 netbuf_data(inbuf, &rq, &len);

 } while(netbuf_next(inbuf) >= 0 );

it is still the same, plus it goes to an infinite loop with all fragments with 
trash.


Do you think this is a buffer reading problem, or is there a connection 
problem ?

(i.e. the bind - listen initialization and then there's the loop for
accept - process - delete sequence. Is there a problem with this?)

Thank you for your time and help,
Bahadir

Here's the code:

void httpd_init(void *arg)
{
  struct netconn *conn, *newconn;

  /* Create a new TCP connection handle. */
  conn = netconn_new(NETCONN_TCP);

  /* Bind the connection to port 80 on any local
  IP address. */
  netconn_bind(conn, NULL, 80);

  /* Put the connection into LISTEN state. */
  netconn_listen(conn);

  /* Loop forever */
  while(1) {

    /* Accept a new connection */
    newconn = netconn_accept(conn);

    /* Process the incoming connection. */
    process_connection(newconn);

    /* Deallocate connection handle */
    netconn_delete(newconn);

  }
}

void process_connection(struct netconn *conn)
{
  struct netbuf *inbuf;
  char *rq;
  u16_t len;

  /* Read data from the connection into the netbuf input buffer.
     We assume that the full request is in the netbuf. */
  inbuf = netconn_recv(conn);

    /* Check if the request was an HTTP "GET /\r\n". */
    if(rq[0] == 'G' && rq[1] == 'E' &&
      rq[2] == 'T') {

        /* Send the header */
        netconn_write(conn, http_html_hdr, sizeof(http_html_hdr),
                      NETCONN_COPY);

        /* Send the actual web page */
        netconn_write(conn, indexdata, sizeof(indexdata),
                      NETCONN_COPY);

        /* Close the connection */
        netconn_close(conn);
        }
}







reply via email to

[Prev in Thread] Current Thread [Next in Thread]