Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
socket

Synchronous socket allocation function based on the specified socket type.

Created sockets are non-blocking and their possible types are either TCP or a UDP sockets. The maximum allowed number of TCP sockets is TCP_SOCK_MAX sockets while the maximum number of UDP sockets that can be created simultaneously is UDP_SOCK_MAX sockets.

Functions

NMI_API SOCKET socket (uint16 u16Domain, uint8 u8Type, uint8 u8Config)
 

NMI_API SOCKET socket ( uint16  u16Domain,
uint8  u8Type,
uint8  u8Config 
)
Parameters
[in]u16DomainSocket family. The only allowed value is AF_INET (IPv4.0) for TCP/UDP sockets.
[in]u8TypeSocket type. Allowed values are:
[in]u8ConfigUsed to specify the socket configuration. The following configuration values are defined:
Precondition
The socketInit function must be called once at the beginning of the application to initialize the socket handler. before any call to the socket function can be made.
See Also
connect secure bind listen accept recv recvfrom send sendto close setsockopt getsockopt
Returns
On successful socket creation, a non-blocking socket type is created and a socket ID is returned In case of failure the function returns a negative value, identifying one of the socket error codes defined. For example: SOCK_ERR_INVALID for invalid argument or SOCK_ERR_MAX_TCP_SOCK if the number of TCP allocated sockets exceeds the number of available sockets.
Remarks
The socket function must be called a priori to any other related socket functions "e.g. send, recv, close ..etc"

Allocation example

This example demonstrates the use of the socket function to allocate the socket, returning the socket handler to be used for other socket operations. Socket creation is dependent on the socket type.

UDP example

SOCKET UdpServerSocket = -1;
UdpServerSocket = socket(AF_INET, SOCK_DGRAM, 0);

TCP example

tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0));

SSL example

static SOCKET ssl_socket = -1;
ssl_socket = socket(AF_INET, SOCK_STREAM, SOCK_FLAGS_SSL));

References AF_INET, tstrSocket::bIsUsed, gu16SessionID, M2M_INFO, m2m_memset(), SOCK_DGRAM, SOCK_STREAM, SOCKET_CMD_SSL_CREATE, SOCKET_CONFIG_SSL_DELAY, SOCKET_CONFIG_SSL_OFF, SOCKET_REQUEST, SSL_FLAGS_ACTIVE, SSL_FLAGS_DELAY, SSL_FLAGS_NO_TX_COPY, tstrSSLSocketCreateCmd::sslSock, TCP_SOCK_MAX, tstrSocket::u16SessionID, tstrSocket::u8SSLFlags, and UDP_SOCK_MAX.

Referenced by connect_cmd_handler(), ConnectNetwork(), http_client_send_request(), main(), NMI_GrowlSendNotification(), smtpConnect(), sslConnect(), and wifi_ap_provision().