so: socket functions

Top  Previous  Next

The socket functions are an API used for transferring (sending and receiving) data across a network.

 

A socket is an abstract representation for the local endpoint of a communication path. The communication path can be across a network to a PC (or similar) or back to the device itself.

This abstraction is introduced so that a single interface can be used for multiple network protocols. (e.g. TCP or UDP)

 

There are two types of sockets:

Stream sockets
Delivery in a networked environment is guaranteed.
If you send through the stream socket three items "A, B, C", they will arrive in the same order − "A, B, C". These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do not have any boundaries.
 

Datagram sockets
Delivery in a networked environment is not guaranteed.
They're connectionless because you don't need to have an open connection as in Stream Sockets − you build a packet with the destination information and send it out. They use UDP (User Datagram Protocol).

 

The images below shows the use of a Stream socket and a Datagram socket:

sockets_tcp   sockets_udp

 

 

The default behavior of the socket functions is to block until the function is completed.

It is possible to configure a socket to be non-blocking, which means that the socket functions return immediately with an error (-4, would block), while the function is completed asynchronous.

 

 

Secure connections:

The socket API have support for secure connections using the Transport Layer Security (TLS) protocol.

This is built into the socket functions, so that no other functions are necessary.

A secure connection can be created both implicit and explicit.

(Implicit being that TLS is negotiated immediately after the connection is established, and Explicit being that TLS is negotiated after communicating on the socket)

 

Secure connections are only available for TCP sockets.

While secure connections can be both blocking and non-blocking, for implicit security the socket MUST be blocking.

(The socket can be switched to non-blocking after the secure connection is established)

 

The image below shows an example of a secure connection:

sockets_tls

 

 

 

Functions for sockets:

 


soOpen

Create a new socket


soClose

Close a socket


soBind

Bind socket to local address


soConnect

Connect a socket to a remote address


soListen

Start listening for incoming connections on socket


soAccept

Accept an incoming connection


soSend

Send data over a socket


soRecv

Read data from a socket


soRecvFrom

Read data from a socket


soStatus

Get the status of a socket


soConfigGet

Read configuration of a socket


soConfigSet

Set configuration of a socket


soConfigTLS

Configure TLS security of a socket


soConfigNonblock

Configure non-blocking state of a socket

 

Option structures:

 


soOptionLinger

The parameters for the linger option.

 

Functions for socket address:

 


soAddrToIP

Convert a socket address to a 32 bit IP address


soAddrFromIP

Convert an IP address to a socket address


soAddrInetGet

Get the IP address and port from a socket address


soAddrInetSet

Create a socket address with an IP address and port


soAddrToInterface

Get the network interface from a socket address


soAddrFromInterface

Get the socket address of a network interface


soAddrLookup

Resolve a domain name or IP address

 

The implementation of sockets have the following limitations:

The maximum number of sockets is limited to 512. The sockets are shared with FTP.