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.

17 BSD SOCKET LIBRARY 17.4 Sending and receiving data<br />

}<br />

break;<br />

case NETCONN_<strong>TCP</strong>:<br />

netconn_write(conn, data, size, NETCONN_COPY);<br />

break;<br />

}<br />

return size;<br />

17.4.2 The sendto() and sendmsg() calls<br />

The sendto() and sendmsg() calls are similar to the send() call, but they allow the application<br />

program to specify the receiver of the data in the parameters to the call. Also, sendto() and<br />

sendmsg() only can be used for UDP connections. The <strong>implementation</strong> uses netconn connect()<br />

to set the receiver of the datagram and must therefore reset the remote <strong>IP</strong> address and port<br />

number if the socket was previously connected. An <strong>implementation</strong> of sendmsg() is not included.<br />

int<br />

sendto(int s, void *data, int size, unsigned int flags,<br />

struct sockaddr *to, int tolen)<br />

{<br />

struct netconn *conn;<br />

struct ip_addr *remote_addr, *addr;<br />

unsigned short remote_port, port;<br />

int ret;<br />

}<br />

conn = sockets[s];<br />

/* get the peer if currently connected */<br />

netconn_peer(conn, &addr, &port);<br />

remote_addr = (struct ip_addr *)to->sin_addr;<br />

remote_port = to->sin_port;<br />

netconn_connect(conn, remote_addr, remote_port);<br />

ret = send(s, data, size, flags);<br />

/* reset the remote address and port number<br />

of the connection */<br />

netconn_connect(conn, addr, port);<br />

17.4.3 The write() call<br />

In the BSD socket API, the write() call sends data on a connection and can be used for both<br />

UDP and <strong>TCP</strong> connections. For <strong>TCP</strong> connections, this maps directly to the <strong>lw<strong>IP</strong></strong> API function<br />

netconn write(). For UDP, the BSD socket function write() function is equvalent to the send()<br />

function.<br />

int<br />

write(int s, void *data, int size)<br />

{<br />

struct netconn *conn;<br />

34

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

Saved successfully!

Ooh no, something went wrong!