netconn_write()

Name

netconn_write() -- Send data on TCP connection

Synopsis

err_t netconn_write(struct netconn *conn, void *data, u16_t len, u8_t copy);

Description

This function is only used for TCP connections. It puts the data pointed to by data on the output queue for the TCP connection conn. The length of the data is given by len. There is no restriction on the length of the data. This function does not require the application to explicitly allocate buffers, as this is taken care of by the stack. The copy parameter has two possible states, as shown below:
#define NETCONN_NOCOPY 0x00
#define NETCONN_COPY 0x01
          

When passed the flag NETCONN_COPY the data is copied into internal buffers which are allocated for the data. This allows the data to be modified directly after the call, but is inefficient both in terms of execution time and memory usage. If the flag NETCONN_NOCOPY is used, the data is not copied but rather referenced. The data must not be modified after the call, since the data can be put on the retransmission queue for the connection, and stay there for an indeterminate amount of time. This is useful when sending data that is located in ROM and therefore is immutable. If greater control over the modifiability of the data is needed, a combination of copied and non-copied data can be used, as seen in the example below.

Example

Example 4-1. This example demonstrates basic usage of the netconn_write() function

Here, the variable data is assumed to be modified later in the program, and is therefore copied into the internal bufiers by passing the flag NETCONN_COPY to netconn_write(). The text variable contains a string that will not be modified and can therefore be sent using references instead of copying.

Note: This is only an example for illustrative purposes, and a complete version should perform comprehensive error checking.

int
main()
{
    struct netconn *conn;
    char data[10];
    char text[] = "Static text";
    int i;

    /* set up the connection conn */
    /* [...] */

    /* create some arbitrary data */
    for(i = 0; i < 10; i++)
        data[i] = i;

    netconn_write(conn, data, 10, NETCONN_COPY);
    netconn_write(conn, text, sizeof(text), NETCONN_NOCOPY);

    /* the data can be modified */
    for(i = 0; i < 10; i++)
        data[i] = 10 - i;

    /* take down the connection conn */
    netconn_close(conn);
}