Base class for IP Based Networking Libraries
Dependencies: DnsQuery
Dependents: TempTower BSDInterfaceTests HelloBSDInterface ESP8266InterfaceTests ... more
You are viewing an older revision! See the latest version
Homepage
Network Socket API¶
The Network Socket API provides a common interface for using sockets on network devices. The API provides a simple class-based interface that should be familiar to users experienced with other socket APIs. Additionally, the API provides a simple interface for implementing network devices, making it easy to connect hardware agnostic programs to new devices.
Usage¶
#include "LWIPInterface.h" #include "TCPSocket.h" LWIPInterface lwip; char buffer[1024]; int main() { TCPSocket sock(&lwip); lwip.connect(); sock.open("www.mbed.org", 80); sock.send("GET / HTTP/1.1\r\n\r\n", 18); sock.recv(buffer, sizeof buffer); sock.close(); lwip.disconnect(); }
Using the Network Socket API involves two core classes
- The Socket class for managing network sockets.
- A device provided DeviceInterface class that implements one of the NetworkInterface classes.
Sockets¶
The Socket class is used for managing network sockets. Once opened, the socket provides a pipe through which data can sent and recieved to a specific endpoint. The socket class can be instantiated as either a TCPSocket or a UDPSocket which defines the protocol used for the connection.
Import library
Public Member Functions |
|
virtual | ~Socket () |
Destroy a socket.
|
|
virtual int | open ( NetworkStack *iface)=0 |
Opens a socket.
|
|
int | close () |
Close the socket.
|
|
int | bind (uint16_t port) |
Bind a specific address to a socket.
|
|
int | bind (const char *address, uint16_t port) |
Bind a specific address to a socket.
|
|
int | bind (const SocketAddress &address) |
Bind a specific address to a socket.
|
|
void | set_blocking (bool blocking) |
Set blocking or non-blocking mode of the socket.
|
|
void | set_timeout (int timeout) |
Set timeout on blocking socket operations.
|
|
void | attach (FunctionPointer callback) |
Register a callback on state change of the socket.
|
|
template<typename T , typename M > | |
void | attach (T *tptr, M mptr) |
Register a callback on state change of the socket.
|
Network Interfaces¶
The NetworkInterface provides an abstract class for network devices that support sockets. Devices should provide a DeviceInterface class that inherits this interface and adds implementation specific methods for using the device. A NetworkInterface must be provided to a Socket constructor to open a socket on the interface. Currently two subclasses are defined for common devices, EthernetInterface and WiFiInterface.