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<br />

17.4.5 The recvfrom() and recvmsg() calls<br />

The recvfrom() and recvmsg() calls are similar to the recv() call but differ in that the <strong>IP</strong><br />

address and port number of the sender of the data can be obtained through the call.<br />

An <strong>implementation</strong> of recvmsg() is not included.<br />

int<br />

recvfrom(int s, void *mem, int len, unsigned int flags,<br />

struct sockaddr *from, int *fromlen)<br />

{<br />

struct netconn *conn;<br />

struct netbuf *buf;<br />

struct ip_addr *addr;<br />

unsigned short port;<br />

int buflen;<br />

}<br />

conn = sockets[s];<br />

buf = netconn_recv(conn);<br />

buflen = netbuf_len(conn);<br />

/* copy the contents of the received buffer into<br />

the supplied memory pointer */<br />

netbuf_copy(buf, mem, len);<br />

addr = netbuf_fromaddr(buf);<br />

port = netbuf_fromport(buf);<br />

from->sin_addr = *addr;<br />

from->sin_port = port;<br />

*fromlen = sizeof(struct sockaddr);<br />

netbuf_delete(buf);<br />

/* if the length of the received data is larger than<br />

len, this data is discarded and we return len.<br />

otherwise we return the actual length of the received<br />

data */<br />

if(len > buflen) {<br />

return buflen;<br />

} else {<br />

return len;<br />

}<br />

18 Code examples<br />

18.1 Using the API<br />

This section presents a simple web server written using the <strong>lw<strong>IP</strong></strong> API. The application code is<br />

given below. The application implements only the bare bones of an HTTP/1.0 [BLFF96] server<br />

and is included only to show the principles in using the <strong>lw<strong>IP</strong></strong> API for an actual application.<br />

The application consists of a single process that accepts connections from the network, responds<br />

to HTTP requests, and closes the connection. There are two functions in the application; main()<br />

36

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

Saved successfully!

Ooh no, something went wrong!