Sockets

This section refers to the following sample application(s):

  • 03-sockets

This application is new to OCSDK 1.2.0

The local module can open a socket and bind it to a specified port - your application can then send and receive on this port.  Your application can also manage ports on its own using the openSocket, and bindSocket APIs.

The 03-sockets Sample Application

The application has the local module open a port - the first well-known user port, f0b8, and prints the sockets opened by the stack and local module. It then opens three sockets and binds the ports starting with the second well-known port, f0b9, and prints all the open sockets:

Sockets app, ver 1.2.0.1
SmartMeshIP stack, ver 1.4.1.6

Checking stack sockets...
socket ID: 0, bindState: 1, port: f0b0
socket ID: 1, bindState: 1, port: f0b1
socket ID: 22, bindState: 1, port: f0b8

Found 3 stack sockets
Opening & binding 3 application sockets...

Listing all sockets:
socket ID: 0, bindState: 1, port: f0b0
socket ID: 1, bindState: 1, port: f0b1
socket ID: 22, bindState: 1, port: f0b8
socket ID: 23, bindState: 1, port: f0b9
socket ID: 24, bindState: 1, port: f0ba
socket ID: 25, bindState: 1, port: f0bb

Implementation Details

The 03-sockets sample application uses the Helper Modules to initialize the CLI Module and for interaction with the local interface. It passes JOIN_NO in the fJoin flag when initializing the local module in the loc_task_init() to prevent the local module from attempting to join.  It has the local module open a socket and bind it to port WKP_USER_1 (f0b8).

It has a single task (socketTask) which:

  • Calls dnm_loc_socketInfoCmd() repeatedly until there are no more sockets, printing each socket it finds. Sockets are referred to by index, so the first error indicates the end of the list. This could be tested explicitly but is assumed in the sample.
  • Adds three application sockets and prints the list of all open sockets.
  • Waits in a while(1) loop.

As with other APIs, the port (a multi-byte field) returned by dnm_loc_socketInfoCmd() is in network order and must be swapped for printing or other operations.

As an exercise, try passing NULL for the udpPort in the loc_task_init().