Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
Socket Class Reference
Socket implements most of the interfaces required for sockets. More...
#include <Socket.h>
Inherited by TCPAsynch, TCPAsynch, UDPSocket, and UDPSocket.
Public Member Functions | |
virtual socket_error_t | resolve (const char *address, const DNSHandler_t &onDNS) |
Start the process of resolving a domain name. | |
virtual socket_error_t | open (const socket_address_family_t af, const socket_proto_family_t pf) |
Open the socket. | |
virtual socket_error_t | bind (const char *address, const uint16_t port) |
Binds the socket's local address and IP. | |
virtual socket_error_t | bind (const SocketAddr *address, const uint16_t port) |
bind(const SocketAddr *, const uint16_t) is the same as bind(const char *, const uint16_t), except that the address passed in is a SocketAddr. | |
virtual void | setOnError (const ErrorHandler_t &onError) |
Set the error handler. | |
virtual void | setOnReadable (const ReadableHandler_t &onReadable) |
Set the received data handler Received data is queued until it is read using recv or recv_from. | |
virtual socket_error_t | recv (void *buf, size_t *len) |
Receive a message. | |
virtual socket_error_t | recv_from (void *buf, size_t *len, SocketAddr *remote_addr, uint16_t *remote_port) |
Receive a message with the sender address and port This API is not valid for SOCK_STREAM. | |
virtual void | setOnSent (const SentHandler_t &onSent) |
Set the onSent handler. | |
virtual socket_error_t | send (const void *buf, const size_t len) |
Send a message Sends a message over an open connection. | |
virtual socket_error_t | send_to (const void *buf, const size_t len, const SocketAddr *remote_addr, uint16_t remote_port) |
Send a message to a specific address and port This API is not valid for SOCK_STREAM. | |
virtual socket_error_t | close () |
Shuts down a socket. | |
virtual bool | error_check (socket_error_t err) |
Error checking utility Generates an event on error, does nothing on SOCKET_ERROR_NONE. | |
virtual bool | isConnected () const |
Checks the socket status to determine whether it is still connected. | |
virtual socket_error_t | getLocalAddr (SocketAddr *addr) const |
Get the local address of the socket if bound. | |
virtual socket_error_t | getLocalPort (uint16_t *port) const |
Get the local port of the socket if bound. | |
virtual socket_error_t | getRemoteAddr (SocketAddr *addr) const |
Get the remote address of the socket if connected. | |
virtual socket_error_t | getRemotePort (uint16_t *port) const |
Get the remote port of the socket if connected. | |
virtual socket_error_t | resolve (const char *address, const DNSHandler_t &onDNS) |
Start the process of resolving a domain name. | |
virtual socket_error_t | open (const socket_address_family_t af, const socket_proto_family_t pf) |
Open the socket. | |
virtual socket_error_t | bind (const char *address, const uint16_t port) |
Binds the socket's local address and IP. | |
virtual socket_error_t | bind (const SocketAddr *address, const uint16_t port) |
bind(const SocketAddr *, const uint16_t) is the same as bind(const char *, const uint16_t), except that the address passed in is a SocketAddr. | |
virtual void | setOnError (const ErrorHandler_t &onError) |
Set the error handler. | |
virtual void | setOnReadable (const ReadableHandler_t &onReadable) |
Set the received data handler Received data is queued until it is read using recv or recv_from. | |
virtual socket_error_t | recv (void *buf, size_t *len) |
Receive a message. | |
virtual socket_error_t | recv_from (void *buf, size_t *len, SocketAddr *remote_addr, uint16_t *remote_port) |
Receive a message with the sender address and port This API is not valid for SOCK_STREAM. | |
virtual void | setOnSent (const SentHandler_t &onSent) |
Set the onSent handler. | |
virtual socket_error_t | send (const void *buf, const size_t len) |
Send a message Sends a message over an open connection. | |
virtual socket_error_t | send_to (const void *buf, const size_t len, const SocketAddr *remote_addr, uint16_t remote_port) |
Send a message to a specific address and port This API is not valid for SOCK_STREAM. | |
virtual socket_error_t | close () |
Shuts down a socket. | |
virtual bool | error_check (socket_error_t err) |
Error checking utility Generates an event on error, does nothing on SOCKET_ERROR_NONE. | |
virtual bool | isConnected () const |
Checks the socket status to determine whether it is still connected. | |
virtual socket_error_t | getLocalAddr (SocketAddr *addr) const |
Get the local address of the socket if bound. | |
virtual socket_error_t | getLocalPort (uint16_t *port) const |
Get the local port of the socket if bound. | |
virtual socket_error_t | getRemoteAddr (SocketAddr *addr) const |
Get the remote address of the socket if connected. | |
virtual socket_error_t | getRemotePort (uint16_t *port) const |
Get the remote port of the socket if connected. | |
Protected Member Functions | |
Socket (const socket_stack_t stack) | |
Socket constructor Initializes the Socket object. | |
virtual | ~Socket () |
Socket destructor Frees the underlying socket implementation. | |
virtual void | _eventHandler (struct socket_event *ev) |
The internal event handler. | |
Socket (const socket_stack_t stack) | |
Socket constructor Initializes the Socket object. | |
virtual | ~Socket () |
Socket destructor Frees the underlying socket implementation. | |
virtual void | _eventHandler (struct socket_event *ev) |
The internal event handler. |
Detailed Description
Socket implements most of the interfaces required for sockets.
Socket is a pure virtual class; it should never be instantiated directly, but it provides common functionality for derived classes.
Definition at line 38 of file features/FEATURE_CLIENT/mbed-client/test/mbedclient/utest/common/sockets/v0/Socket.h.
Constructor & Destructor Documentation
Socket | ( | const socket_stack_t | stack ) | [protected] |
Socket constructor Initializes the Socket object.
Initializes the underlying struct socket. Does not instantiate an underlying network stack socket. Since it is somewhat awkward to provide the network stack, a future change will provide a way to pass the network interface to the socket constructor, which will extract the stack from the interface.
- Parameters:
-
[in] stack The network stack to use for this socket.
Definition at line 255 of file test/mbedclient/utest/stub/common_stub.cpp.
~Socket | ( | ) | [protected, virtual] |
Socket destructor Frees the underlying socket implementation.
Definition at line 260 of file test/mbedclient/utest/stub/common_stub.cpp.
Socket | ( | const socket_stack_t | stack ) | [protected] |
Socket constructor Initializes the Socket object.
Initializes the underlying struct socket. Does not instantiate an underlying network stack socket. Since it is somewhat awkward to provide the network stack, a future change will provide a way to pass the network interface to the socket constructor, which will extract the stack from the interface.
- Parameters:
-
[in] stack The network stack to use for this socket.
virtual ~Socket | ( | ) | [protected, virtual] |
Socket destructor Frees the underlying socket implementation.
Member Function Documentation
void _eventHandler | ( | struct socket_event * | ev ) | [protected, virtual] |
The internal event handler.
- Parameters:
-
[in] ev The event to handle
Reimplemented in TCPListener, and TCPListener.
Definition at line 274 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual void _eventHandler | ( | struct socket_event * | ev ) | [protected, virtual] |
The internal event handler.
- Parameters:
-
[in] ev The event to handle
Reimplemented in TCPListener, and TCPListener.
socket_error_t bind | ( | const char * | address, |
const uint16_t | port | ||
) | [virtual] |
Binds the socket's local address and IP.
0.0.0.0 is accepted as a local address if only the port is meant to be bound. The behaviour of bind("0.0.0.0",...) is undefined where two or more stacks are in use.
- Parameters:
-
[in] address The string representation of the address to bind [in] port The local port to bind
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
Definition at line 319 of file test/mbedclient/utest/stub/common_stub.cpp.
socket_error_t bind | ( | const SocketAddr * | address, |
const uint16_t | port | ||
) | [virtual] |
bind(const SocketAddr *, const uint16_t) is the same as bind(const char *, const uint16_t), except that the address passed in is a SocketAddr.
- Parameters:
-
[in] address The address to bind [in] port The local port to bind
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
Definition at line 323 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual socket_error_t bind | ( | const char * | address, |
const uint16_t | port | ||
) | [virtual] |
Binds the socket's local address and IP.
0.0.0.0 is accepted as a local address if only the port is meant to be bound. The behaviour of bind("0.0.0.0",...) is undefined where two or more stacks are in use.
- Parameters:
-
[in] address The string representation of the address to bind [in] port The local port to bind
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
virtual socket_error_t bind | ( | const SocketAddr * | address, |
const uint16_t | port | ||
) | [virtual] |
bind(const SocketAddr *, const uint16_t) is the same as bind(const char *, const uint16_t), except that the address passed in is a SocketAddr.
- Parameters:
-
[in] address The address to bind [in] port The local port to bind
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
virtual socket_error_t close | ( | ) | [virtual] |
Shuts down a socket.
Sending and receiving are no longer possible after close() is called. The socket is not deallocated on close. A socket must not be reopened, it should be destroyed (either with delete, or by going out of scope) after calling close. Calling open on a closed socket can result in a memory leak.
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
socket_error_t close | ( | ) | [virtual] |
Shuts down a socket.
Sending and receiving are no longer possible after close() is called. The socket is not deallocated on close. A socket must not be reopened, it should be destroyed (either with delete, or by going out of scope) after calling close. Calling open on a closed socket can result in a memory leak.
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
Definition at line 328 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual bool error_check | ( | socket_error_t | err ) | [virtual] |
Error checking utility Generates an event on error, does nothing on SOCKET_ERROR_NONE.
- Parameters:
-
[in] err the error code to check
- Returns:
- false if err is SOCKET_ERROR_NONE, true otherwise
bool error_check | ( | socket_error_t | err ) | [virtual] |
Error checking utility Generates an event on error, does nothing on SOCKET_ERROR_NONE.
- Parameters:
-
[in] err the error code to check
- Returns:
- false if err is SOCKET_ERROR_NONE, true otherwise
Definition at line 269 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual socket_error_t getLocalAddr | ( | SocketAddr * | addr ) | const [virtual] |
Get the local address of the socket if bound.
There are several failing conditions for this method: 1. If the socket has not been opened, returns SOCKET_ERROR_NULL_PTR 2. If the socket has not been bound, returns SOCKET_ERROR_NOT_BOUND 3. If addr is NULL, returns SOCKET_ERROR_NULL_PTR
Otherwise, populates the SocketAddr object with the local address
- Parameters:
-
[out] addr a pointer to a SocketAddr object
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure (see description)
socket_error_t getLocalAddr | ( | SocketAddr * | addr ) | const [virtual] |
Get the local address of the socket if bound.
There are several failing conditions for this method: 1. If the socket has not been opened, returns SOCKET_ERROR_NULL_PTR 2. If the socket has not been bound, returns SOCKET_ERROR_NOT_BOUND 3. If addr is NULL, returns SOCKET_ERROR_NULL_PTR
Otherwise, populates the SocketAddr object with the local address
- Parameters:
-
[out] addr a pointer to a SocketAddr object
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure (see description)
Definition at line 294 of file test/mbedclient/utest/stub/common_stub.cpp.
socket_error_t getLocalPort | ( | uint16_t * | port ) | const [virtual] |
Get the local port of the socket if bound.
There are several failing conditions for this method: 1. If the socket has not been opened, returns SOCKET_ERROR_NULL_PTR 2. If the socket has not been bound, returns SOCKET_ERROR_NOT_BOUND 3. If port is NULL, returns SOCKET_ERROR_NULL_PTR
Otherwise, populates the uint16_t object with the local port
- Parameters:
-
[out] port a pointer to a uint16_t
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure (see description)
Definition at line 299 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual socket_error_t getLocalPort | ( | uint16_t * | port ) | const [virtual] |
Get the local port of the socket if bound.
There are several failing conditions for this method: 1. If the socket has not been opened, returns SOCKET_ERROR_NULL_PTR 2. If the socket has not been bound, returns SOCKET_ERROR_NOT_BOUND 3. If port is NULL, returns SOCKET_ERROR_NULL_PTR
Otherwise, populates the uint16_t object with the local port
- Parameters:
-
[out] port a pointer to a uint16_t
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure (see description)
socket_error_t getRemoteAddr | ( | SocketAddr * | addr ) | const [virtual] |
Get the remote address of the socket if connected.
There are several failing conditions for this method: 1. If the socket has not been opened, returns SOCKET_ERROR_NULL_PTR 2. If the socket has not been connected, returns SOCKET_ERROR_NO_CONNECTION 3. If addr is NULL, returns SOCKET_ERROR_NULL_PTR
Otherwise, populates the SocketAddr object with the remote address
- Parameters:
-
[out] addr a pointer to a SocketAddr object
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure (see description)
Definition at line 304 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual socket_error_t getRemoteAddr | ( | SocketAddr * | addr ) | const [virtual] |
Get the remote address of the socket if connected.
There are several failing conditions for this method: 1. If the socket has not been opened, returns SOCKET_ERROR_NULL_PTR 2. If the socket has not been connected, returns SOCKET_ERROR_NO_CONNECTION 3. If addr is NULL, returns SOCKET_ERROR_NULL_PTR
Otherwise, populates the SocketAddr object with the remote address
- Parameters:
-
[out] addr a pointer to a SocketAddr object
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure (see description)
socket_error_t getRemotePort | ( | uint16_t * | port ) | const [virtual] |
Get the remote port of the socket if connected.
There are several failing conditions for this method: 1. If the socket has not been opened, returns SOCKET_ERROR_NULL_PTR 2. If the socket has not been connected, returns SOCKET_ERROR_NO_CONNECTION 3. If port is NULL, returns SOCKET_ERROR_NULL_PTR
Otherwise, populates the uint16_t object with the remote port
- Parameters:
-
[out] port a pointer to a uint16_t
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure (see description)
Definition at line 309 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual socket_error_t getRemotePort | ( | uint16_t * | port ) | const [virtual] |
Get the remote port of the socket if connected.
There are several failing conditions for this method: 1. If the socket has not been opened, returns SOCKET_ERROR_NULL_PTR 2. If the socket has not been connected, returns SOCKET_ERROR_NO_CONNECTION 3. If port is NULL, returns SOCKET_ERROR_NULL_PTR
Otherwise, populates the uint16_t object with the remote port
- Parameters:
-
[out] port a pointer to a uint16_t
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure (see description)
virtual bool isConnected | ( | ) | const [virtual] |
Checks the socket status to determine whether it is still connected.
- Returns:
- true if the socket is connected, false if it is not
bool isConnected | ( | ) | const [virtual] |
Checks the socket status to determine whether it is still connected.
- Returns:
- true if the socket is connected, false if it is not
Definition at line 351 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual socket_error_t open | ( | const socket_address_family_t | af, |
const socket_proto_family_t | pf | ||
) | [virtual] |
Open the socket.
Instantiates and initializes the underlying socket. Receive is started immediately after the socket is opened.
- Parameters:
-
[in] af Address family (SOCKET_AF_INET4 or SOCKET_AF_INET6), currently only IPv4 is supported [in] pf Protocol family (SOCKET_DGRAM or SOCKET_STREAM)
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
socket_error_t open | ( | const socket_address_family_t | af, |
const socket_proto_family_t | pf | ||
) | [virtual] |
Open the socket.
Instantiates and initializes the underlying socket. Receive is started immediately after the socket is opened.
- Parameters:
-
[in] af Address family (SOCKET_AF_INET4 or SOCKET_AF_INET6), currently only IPv4 is supported [in] pf Protocol family (SOCKET_DGRAM or SOCKET_STREAM)
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
Definition at line 264 of file test/mbedclient/utest/stub/common_stub.cpp.
socket_error_t recv | ( | void * | buf, |
size_t * | len | ||
) | [virtual] |
Receive a message.
- Parameters:
-
[out] buf The buffer to fill [in,out] len A pointer to the size of the receive buffer. Sets the maximum number of bytes to read but is updated with the actual number of bytes copied on success. len is not changed on failure
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
Definition at line 333 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual socket_error_t recv | ( | void * | buf, |
size_t * | len | ||
) | [virtual] |
Receive a message.
- Parameters:
-
[out] buf The buffer to fill [in,out] len A pointer to the size of the receive buffer. Sets the maximum number of bytes to read but is updated with the actual number of bytes copied on success. len is not changed on failure
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
socket_error_t recv_from | ( | void * | buf, |
size_t * | len, | ||
SocketAddr * | remote_addr, | ||
uint16_t * | remote_port | ||
) | [virtual] |
Receive a message with the sender address and port This API is not valid for SOCK_STREAM.
- Parameters:
-
[out] buf The buffer to fill [in,out] len A pointer to the size of the receive buffer. Sets the maximum number of bytes to read but is updated with the actual number of bytes copied on success. len is not changed on failure [out] remote_addr Pointer to an address structure to fill with the sender address [out] remote_port Pointer to a uint16_t to fill with the sender port
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
Definition at line 337 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual socket_error_t recv_from | ( | void * | buf, |
size_t * | len, | ||
SocketAddr * | remote_addr, | ||
uint16_t * | remote_port | ||
) | [virtual] |
Receive a message with the sender address and port This API is not valid for SOCK_STREAM.
- Parameters:
-
[out] buf The buffer to fill [in,out] len A pointer to the size of the receive buffer. Sets the maximum number of bytes to read but is updated with the actual number of bytes copied on success. len is not changed on failure [out] remote_addr Pointer to an address structure to fill with the sender address [out] remote_port Pointer to a uint16_t to fill with the sender port
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
socket_error_t resolve | ( | const char * | address, |
const DNSHandler_t & | onDNS | ||
) | [virtual] |
Start the process of resolving a domain name.
If the input is a text IP address, an event is queued immediately; otherwise, onDNS is queued as soon as DNS is resolved. The socket must have been opened before resolve is called
- Parameters:
-
[in] address The domain name to resolve [in] onDNS The handler to call when the name is resolved
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
Definition at line 314 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual socket_error_t resolve | ( | const char * | address, |
const DNSHandler_t & | onDNS | ||
) | [virtual] |
Start the process of resolving a domain name.
If the input is a text IP address, an event is queued immediately; otherwise, onDNS is queued as soon as DNS is resolved. The socket must have been opened before resolve is called
- Parameters:
-
[in] address The domain name to resolve [in] onDNS The handler to call when the name is resolved
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
virtual socket_error_t send | ( | const void * | buf, |
const size_t | len | ||
) | [virtual] |
Send a message Sends a message over an open connection.
This call is valid for UDP sockets, provided that connect() has been called.
- Parameters:
-
[in] buf The payload to send [in] len The size of the payload
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
socket_error_t send | ( | const void * | buf, |
const size_t | len | ||
) | [virtual] |
Send a message Sends a message over an open connection.
This call is valid for UDP sockets, provided that connect() has been called.
- Parameters:
-
[in] buf The payload to send [in] len The size of the payload
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
Definition at line 342 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual socket_error_t send_to | ( | const void * | buf, |
const size_t | len, | ||
const SocketAddr * | remote_addr, | ||
uint16_t | remote_port | ||
) | [virtual] |
Send a message to a specific address and port This API is not valid for SOCK_STREAM.
- Parameters:
-
[in] buf The payload to send [in] len The size of the payload [in] address The address to send to [in] port The remote port to send to
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
socket_error_t send_to | ( | const void * | buf, |
const size_t | len, | ||
const SocketAddr * | remote_addr, | ||
uint16_t | remote_port | ||
) | [virtual] |
Send a message to a specific address and port This API is not valid for SOCK_STREAM.
- Parameters:
-
[in] buf The payload to send [in] len The size of the payload [in] address The address to send to [in] port The remote port to send to
- Returns:
- SOCKET_ERROR_NONE on success, or an error code on failure
Definition at line 346 of file test/mbedclient/utest/stub/common_stub.cpp.
void setOnError | ( | const ErrorHandler_t & | onError ) | [virtual] |
Set the error handler.
Errors are ignored if onError is not set.
- Parameters:
-
[in] onError
Definition at line 278 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual void setOnError | ( | const ErrorHandler_t & | onError ) | [virtual] |
Set the error handler.
Errors are ignored if onError is not set.
- Parameters:
-
[in] onError
virtual void setOnReadable | ( | const ReadableHandler_t & | onReadable ) | [virtual] |
Set the received data handler Received data is queued until it is read using recv or recv_from.
- Parameters:
-
[in] onReadable the handler to use for receive events
void setOnReadable | ( | const ReadableHandler_t & | onReadable ) | [virtual] |
Set the received data handler Received data is queued until it is read using recv or recv_from.
- Parameters:
-
[in] onReadable the handler to use for receive events
Definition at line 282 of file test/mbedclient/utest/stub/common_stub.cpp.
virtual void setOnSent | ( | const SentHandler_t & | onSent ) | [virtual] |
Set the onSent handler.
The exact moment this handler is called varies from implementation to implementation. On LwIP, onSent is called when the remote host ACK's data in TCP sockets, or when the message enters the network stack in UDP sockets.
- Parameters:
-
[in] onSent The handler to call when a send completes
void setOnSent | ( | const SentHandler_t & | onSent ) | [virtual] |
Set the onSent handler.
The exact moment this handler is called varies from implementation to implementation. On LwIP, onSent is called when the remote host ACK's data in TCP sockets, or when the message enters the network stack in UDP sockets.
- Parameters:
-
[in] onSent The handler to call when a send completes
Definition at line 286 of file test/mbedclient/utest/stub/common_stub.cpp.
Generated on Tue Jul 12 2022 11:02:16 by
