26.12.2013 Views

lwIP - A Minimal TCP/IP implementation - Wikia

lwIP - A Minimal TCP/IP implementation - Wikia

lwIP - A Minimal TCP/IP implementation - Wikia

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

18 CODE EXAMPLES 18.1 Using the API<br />

netbuf_data(inbuf, &rq, &len);<br />

/* Check if the request was an HTTP "GET /\r\n". */<br />

if(rq[0] == ’G’ && rq[1] == ’E’ &&<br />

rq[2] == ’T’ && rq[3] == ’ ’ &&<br />

rq[4] == ’/’ && rq[5] == ’\r’ &&<br />

rq[6] == ’\n’) {<br />

/* Send the header. */<br />

netconn_write(conn, http_html_hdr, sizeof(http_html_hdr),<br />

NETCONN_NOCOPY);<br />

/* Send the actual web page. */<br />

netconn_write(conn, indexdata, sizeof(indexdata),<br />

NETCONN_NOCOPY);<br />

}<br />

}<br />

}<br />

/* Close the connection. */<br />

netconn_close(conn);<br />

/* The main() function. */<br />

int<br />

main()<br />

{<br />

struct netconn *conn, *newconn;<br />

}<br />

/* Create a new <strong>TCP</strong> connection handle. */<br />

conn = netconn_new(NETCONN_<strong>TCP</strong>);<br />

/* Bind the connection to port 80 on any<br />

local <strong>IP</strong> address. */<br />

netconn_bind(conn, NULL, 80);<br />

/* Put the connection into LISTEN state. */<br />

netconn_listen(conn);<br />

/* Loop forever. */<br />

while(1) {<br />

/* Accept a new connection. */<br />

newconn = netconn_accept(conn);<br />

/* Process the incomming connection. */<br />

process_connection(newconn);<br />

/* Deallocate connection handle. */<br />

netconn_delete(newconn);<br />

}<br />

return 0;<br />

38

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!