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.

16 NETWORK CONNECTION FUNCTIONS<br />

}<br />

/* the connection has now been closed by the<br />

other end, so we close our end */<br />

netconn_close(conn);<br />

16.0.24 netconn write()<br />

Synopsis<br />

int netconn write(struct netconn *conn, void *data,<br />

int len, unsigned int flags)<br />

Description<br />

This function is only used for <strong>TCP</strong> connections. It puts the data pointed to by data on the output<br />

queue for the <strong>TCP</strong> connection conn. The length of the data is given by len. There is no restriction<br />

on the length of the data. This function does not require the application to explicitly allocate<br />

buffers, as this is taken care of by the stack. The flags parameter has two possible states, as<br />

shown below.<br />

#define NETCONN_NOCOPY 0x00<br />

#define NETCONN_COPY 0x01<br />

When passed the flag NETCONN_COPY the data is copied into internal buffers which is allocated<br />

for the data. This allows the data to be modified directly after the call, but is inefficient both in<br />

terms of execution time and memory usage. If the flag NETCONN_NOCOPY is used, the data is not<br />

copied but rather referenced. The data must not be modified after the call, since the data can be<br />

put on the retransmission queue for the connection, and stay there for an indeterminate amount<br />

of time. This is useful when sending data that is located in ROM and therefore is immutable.<br />

If greater control over the modifiability of the data is needed, a combination of copied and<br />

non-copied data can be used, as seen in the example below.<br />

Example This example shows the basic usage of netconn_write(). Here, the variable data is<br />

assumed to be modified later in the program, and is therefore copied into the internal buffers by<br />

passing the flag NETCONN_COPY to netconn_write(). The text variable contains a string that will<br />

not be modified and can therefore be sent using references instead of copying.<br />

int<br />

main()<br />

{<br />

struct netconn *conn;<br />

char data[10];<br />

char text[] = "Static text";<br />

int i;<br />

/* set up the connection conn */<br />

/* [...] */<br />

/* create some arbitrary data */<br />

for(i = 0; i < 10; i++)<br />

data[i] = i;<br />

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

netconn_write(conn, text, sizeof(text), NETCONN_NOCOPY);<br />

28

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

Saved successfully!

Ooh no, something went wrong!