Chapter 4. API Guides
References
A wide range of BSD Sockets reference material is available, including:
• Single UNIX Specification BSD Sockets page
• Berkeley Sockets Wikipedia page
Examples
A number of ESP-IDF examples show how to use the BSD Sockets APIs:
• protocols/sockets/tcp_server
• protocols/sockets/tcp_client
• protocols/sockets/udp_server
• protocols/sockets/udp_client
• protocols/sockets/udp_multicast
• protocols/http_request (Note: this is a simplified example of using a TCP socket to send an HTTP request.
The ESP HTTP Client is a much better option for sending HTTP requests.)
Supported functions
The following BSD socket API functions are supported. For full details see lwip/lwip/src/include/lwip/sockets.h.
• socket()
• bind()
• accept()
• shutdown()
• getpeername()
• getsockopt() & setsockopt() (see Socket Options)
• close() (via Virtual filesystem component)
• read(), readv(), write(), writev() (via Virtual filesystem component)
• recv(), recvmsg(), recvfrom()
• send(), sendmsg(), sendto()
• select() (via Virtual filesystem component)
• poll() (Note: on ESP-IDF, poll() is implemented by calling select internally, so using select()
directly is recommended if a choice of methods is available.)
• fcntl() (see fcntl)
Non-standard functions:
• ioctl() (see ioctls)
Note: Some lwIP application sample code uses prefixed versions of BSD APIs, for example lwip_socket()
instead of the standard socket(). Both forms can be used with ESP-IDF, but using standard names is recom-
mended.
Socket Error Handling
BSD Socket error handling code is very important for robust socket applications. Normally the socket error handling
involves the following aspects:
• Detecting the error.
• Geting the error reason code.
• Handle the error according to the reason code.
In lwIP, we have two different scenarios of handling socket errors:
• Socket API returns an error. For more information, see Socket API Errors.
Espressif Systems 1425
Submit Document Feedback
Release v4.4