zephyr/samples/net/sockets/socketpair
Keith Packard 0b90fd5adf samples, tests, boards: Switch main return type from void to int
As both C and C++ standards require applications running under an OS to
return 'int', adapt that for Zephyr to align with those standard. This also
eliminates errors when building with clang when not using -ffreestanding,
and reduces the need for compiler flags to silence warnings for both clang
and gcc.

Most of these changes were automated using coccinelle with the following
script:

@@
@@
- void
+ int
main(...) {
	...
-	return;
+	return 0;
	...
}

Approximately 40 files had to be edited by hand as coccinelle was unable to
fix them.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-14 07:49:41 +09:00
..
src samples, tests, boards: Switch main return type from void to int 2023-04-14 07:49:41 +09:00
CMakeLists.txt
Makefile.posix samples: net: socketpair: rename to main.c for consistence 2023-02-08 19:04:25 +09:00
prj.conf samples: net: socketpair: clean up sample app 2023-02-08 19:04:25 +09:00
README.rst samples: net: socketpair: clean up sample app 2023-02-08 19:04:25 +09:00
sample.yaml samples: net: socketpair: clean up sample app 2023-02-08 19:04:25 +09:00

.. _sockets-socketpair-sample:

Socketpair Example
##################

Overview
********

The sockets/socketpair sample application for Zephyr demonstrates a
multi-threaded application communicating over pairs of unnamed,
connected UNIX-domain sockets. The pairs of sockets are created with
socketpair(2), as you might have guessed. Such sockets are compatible
with the BSD Sockets API, and therefore the purpose of this sample
is also to reinforce that it is possible to develop a sockets
application portable to both POSIX and Zephyr.

The source code for this sample application can be found at:
:zephyr_file:`samples/net/sockets/socketpair`.

Requirements
************

None

Building and Running
********************

Build the Zephyr version of the sockets/echo application like this:

.. zephyr-app-commands::
   :zephyr-app: samples/net/sockets/socketpair
   :board: <board_to_use>
   :goals: build
   :compact:

After the sample starts, several client threads are spawned. Each client
thread sends a fixed number of messages to the server (main).

.. code-block:: console

    *** Booting Zephyr OS build v3.3.0-rc1-97-g432ff20a72e1 ***
    setting-up
    Alpha: socketpair: 4 <=> 3
    Bravo: socketpair: 6 <=> 5
    Charlie: socketpair: 8 <=> 7
    main: read 'Alpha' on fd 4
    main: read 'Bravo' on fd 6
    main: read 'Charlie' on fd 8
    main: read 'Alpha' on fd 4
    main: read 'Bravo' on fd 6
    main: read 'Charlie' on fd 8
    main: read 'Alpha' on fd 4
    main: read 'Bravo' on fd 6
    main: read 'Charlie' on fd 8
    tearing-down
    SUCCESS

Running application on POSIX Host
=================================

The same application source code can be built for a POSIX system, e.g.
Linux.

To build for a host POSIX OS:

.. code-block:: console

    $ make -f Makefile.posix

To run:

.. code-block:: console

    ./socketpair_example
    setting-up
    Alpha: socketpair: 3 <=> 4
    Bravo: socketpair: 5 <=> 6
    Charlie: socketpair: 7 <=> 8
    main: read 'Alpha' on fd 3
    main: read 'Bravo' on fd 5
    main: read 'Charlie' on fd 7
    main: read 'Alpha' on fd 3
    main: read 'Alpha' on fd 3
    main: read 'Bravo' on fd 5
    main: read 'Charlie' on fd 7
    main: read 'Bravo' on fd 5
    main: read 'Charlie' on fd 7
    tearing-down
    SUCCESS

As can be seen, the behavior of the application is approximately the same as
the Zephyr version.