ThingWorx Edge C SDK > Porting to Another Platform > Socket Functions
Socket Functions
The C SDK does not include a TCP/IP stack. Rather, it assumes that the underlying platform provides that functionality. To that end, the SDK has defined a series of wrapper functions to mask the underlying native socket functions. The function definitions use an underlying twSocket structure that abstracts away some of the differences in how certain platforms deal with socket descriptors – for example, Linux uses an int while Windows uses a HANDLE. The structure is defined in the file, src/porting/twOSPort.h, as follows:
typedef struct twSocket {
TW_SOCKET_TYPE sock; /* socket descriptor */
TW_ADDR_INFO addr; /* address to use */
TW_ADDR_INFO * addrInfo; /* Addr Info struct head - use to free */
char state;
} twSocket;
The actual definition of and TW_ADDR_INFO and the implementation of the functions above should be done in your platform-specific C file. The following table lists and describes the socket functions that must be provided by a port. For signatures, parameter details, and return information, refer to the Doxygen documentation provided with the SDK.
Allocate and initialize a socket structure.
Establish a connection to the specified host/port pair.
Re-establish a connection to the specified host/port pair. The underlying socket will be torn down and recreated, but all other twSocket parameters should remain intact.
Close a previously opened connection.
Check to see if data is available on a socket. Use this function to prevent a twSocket_Read call from blocking permanently if no data is available. This function is especially important if using the built-in tasker, which cannot have tasks that block.
Read data from a socket.
Write data to a socket.
Delete a twSocket structure. This function should close the socket if it is still open before deleting the structure.
Get the error code of the last error that occurred while using a socket. Note that this is typically a system-wide call and not a call to a specific socket.