2016-08-26 18:15:00 +08:00
.. _networking_api_usage:
Network Connectivity API
########################
Applications can use the connectivity API defined in :file: `net_context.h`
to create a connection, send or receive data, and close a connection.
The same API can be used when working with UDP or TCP data.
The net_context API is similar to the BSD socket API and mapping between these
two is possible. The main difference between net_context API and BSD socket
API is that the net_context API uses the fragmented network buffers (net_buf)
2017-04-08 03:57:26 +08:00
defined in :file: `include/net/buf.h` and BSD socket API uses linear memory buffers.
2016-08-26 18:15:00 +08:00
This example creates a simple server that listens to incoming UDP connections
2017-02-28 05:15:43 +08:00
and sends the received data back. You can download the example application
2017-04-29 04:13:53 +08:00
source file here `connectivity-example-app.c <https://raw.githubusercontent.com/zephyrproject-rtos/zephyr/master/doc/subsystems/networking/connectivity-example-app.c> `_
2016-08-26 18:15:00 +08:00
This example application begins with some initialization. (Use this as an
example; you may need to do things differently in your own application.)
.. literalinclude :: connectivity-example-app.c
:linenos:
:language: c
:lines: 2-54
2017-06-08 08:25:28 +08:00
:lineno-start: 2
2016-08-26 18:15:00 +08:00
After initialization, first thing application needs to create a context.
Context is similar to a socket.
.. literalinclude :: connectivity-example-app.c
:linenos:
:language: c
:lines: 57-66
2017-06-08 08:25:28 +08:00
:lineno-start: 57
2016-08-26 18:15:00 +08:00
Then you need to define the local end point for a connection.
.. literalinclude :: connectivity-example-app.c
:linenos:
:language: c
:lines: 69-83
2017-06-08 08:25:28 +08:00
:lineno-start: 69
2016-08-26 18:15:00 +08:00
Wait until the connection data is received.
.. literalinclude :: connectivity-example-app.c
:linenos:
:language: c
2016-10-27 02:51:31 +08:00
:lines: 86-202
2017-06-08 08:25:28 +08:00
:lineno-start: 86
2016-08-26 18:15:00 +08:00
Close the context when finished.
.. literalinclude :: connectivity-example-app.c
:linenos:
:language: c
2017-06-08 08:25:28 +08:00
:lines: 204-214
:lineno-start: 204
2016-08-26 18:15:00 +08:00
.. toctree ::
:maxdepth: 1