Base class for IP Based Networking Libraries

Dependencies:   DnsQuery

Dependents:   TempTower BSDInterfaceTests HelloBSDInterface ESP8266InterfaceTests ... more

For a complete getting started guide see the wiki...

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.

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.

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.

Committer:
Christopher Haster
Date:
Tue Apr 05 09:16:47 2016 -0500
Revision:
77:b66a6984ed2d
Parent:
76:bbe51641f405
Child:
78:0914f9b9b24b
Integrated interfaces as arguments to sockets

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 72:6a8b52ee83ed 1 /* Copyright (C) 2012 mbed.org, MIT License
Christopher Haster 72:6a8b52ee83ed 2 *
Christopher Haster 72:6a8b52ee83ed 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Christopher Haster 72:6a8b52ee83ed 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Christopher Haster 72:6a8b52ee83ed 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Christopher Haster 72:6a8b52ee83ed 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Christopher Haster 72:6a8b52ee83ed 7 * furnished to do so, subject to the following conditions:
Christopher Haster 72:6a8b52ee83ed 8 *
Christopher Haster 72:6a8b52ee83ed 9 * The above copyright notice and this permission notice shall be included in all copies or
Christopher Haster 72:6a8b52ee83ed 10 * substantial portions of the Software.
Christopher Haster 72:6a8b52ee83ed 11 *
Christopher Haster 72:6a8b52ee83ed 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Christopher Haster 72:6a8b52ee83ed 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Christopher Haster 72:6a8b52ee83ed 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Christopher Haster 72:6a8b52ee83ed 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Christopher Haster 72:6a8b52ee83ed 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Christopher Haster 72:6a8b52ee83ed 17 */
Christopher Haster 72:6a8b52ee83ed 18 #ifndef SOCKET_H
Christopher Haster 72:6a8b52ee83ed 19 #define SOCKET_H
Christopher Haster 72:6a8b52ee83ed 20
Christopher Haster 74:ef2470ca328b 21
Christopher Haster 72:6a8b52ee83ed 22 /** Abstract socket class
Christopher Haster 74:ef2470ca328b 23 */
Christopher Haster 72:6a8b52ee83ed 24 class Socket {
Christopher Haster 72:6a8b52ee83ed 25 public:
Christopher Haster 72:6a8b52ee83ed 26 /** Socket lifetime
Christopher Haster 72:6a8b52ee83ed 27 */
Christopher Haster 72:6a8b52ee83ed 28 Socket();
Christopher Haster 72:6a8b52ee83ed 29 ~Socket();
Christopher Haster 72:6a8b52ee83ed 30
Christopher Haster 72:6a8b52ee83ed 31 /** Set blocking or non-blocking mode of the socket
Christopher Haster 72:6a8b52ee83ed 32 \param blocking true for blocking mode, false for non-blocking mode.
Christopher Haster 72:6a8b52ee83ed 33 */
Christopher Haster 72:6a8b52ee83ed 34 void set_blocking(bool blocking);
Christopher Haster 72:6a8b52ee83ed 35
Christopher Haster 74:ef2470ca328b 36 /** Set timeout on a socket operation if blocking behaviour is enabled
Christopher Haster 72:6a8b52ee83ed 37 \param timeout timeout in ms
Christopher Haster 72:6a8b52ee83ed 38 */
Christopher Haster 72:6a8b52ee83ed 39 void set_timeout(unsigned int timeout);
Christopher Haster 72:6a8b52ee83ed 40
Christopher Haster 72:6a8b52ee83ed 41 /** Set socket options
Christopher Haster 76:bbe51641f405 42 \param optname Option ID
Christopher Haster 76:bbe51641f405 43 \param optval Option value
Christopher Haster 76:bbe51641f405 44 \param optlen Length of the option value
Christopher Haster 76:bbe51641f405 45 \return 0 on success, negative on failure
Christopher Haster 72:6a8b52ee83ed 46 */
Christopher Haster 74:ef2470ca328b 47 int set_option(int optname, const void *optval, unsigned int optlen);
Christopher Haster 72:6a8b52ee83ed 48
Christopher Haster 72:6a8b52ee83ed 49 /** Get socket options
Christopher Haster 76:bbe51641f405 50 \param optname Option ID
Christopher Haster 76:bbe51641f405 51 \param optval Buffer pointer where to write the option value
Christopher Haster 76:bbe51641f405 52 \param optlen Length of the option value
Christopher Haster 76:bbe51641f405 53 \return 0 on success, negative on failure
Christopher Haster 72:6a8b52ee83ed 54 */
Christopher Haster 74:ef2470ca328b 55 int get_option(int optname, void *optval, unsigned int *optlen);
Christopher Haster 72:6a8b52ee83ed 56
Christopher Haster 72:6a8b52ee83ed 57 /** Close the socket
Christopher Haster 72:6a8b52ee83ed 58 \param shutdown free the left-over data in message queues
Christopher Haster 72:6a8b52ee83ed 59 */
Christopher Haster 72:6a8b52ee83ed 60 int close(bool shutdown=true);
Christopher Haster 72:6a8b52ee83ed 61 };
Christopher Haster 72:6a8b52ee83ed 62
Christopher Haster 72:6a8b52ee83ed 63 #endif