Mistake on this page?
Report an issue in GitHub or email us


EthInterface class hierarchy

The EthInterface provides a C++ API for connecting to the internet over Ethernet. By default, this class does not require any configuration. It is able to pick up the default Ethernet driver for the target and select correct network stack.


To statically initialize the driver, create an object without passing any parameters:

EthernetInterface eth;

Then, if the default configuration is enough, bring up the interface:

nsapi_error_t status = eth.connect();

Now, the interface is ready to be used for network sockets.

// Open a TCP socket
TCPSocket socket;

// Open a UDP socket
UDPSocket socket;


For EthernetInterface, there are two possible configurations:

  • Use DHCP for network addressing. This is the default.
  • Use statically configured IP addresses.

Refer to the API below for how to set the IP addresses by calling the set_network() function.

Troubleshooting information

Network interface connect failure causes:

  • NSAPI_ERROR_NO_CONNECTION indicates that the Ethernet link up has failed. Check that the Ethernet connection is working.
  • NSAPI_ERROR_DHCP_FAILURE indicates that acquiring the IP address has failed. Check that the IP address configuration service is working.

EthInterface class reference

Public Member Functions
virtual EthInterfaceethInterface ()
 Public Member Functions inherited from NetworkInterface
virtual const char * get_mac_address ()
virtual const char * get_ip_address ()
virtual const char * get_netmask ()
virtual const char * get_gateway ()
virtual nsapi_error_t set_network (const char *ip_address, const char *netmask, const char *gateway)
virtual nsapi_error_t set_dhcp (bool dhcp)
virtual nsapi_error_t connect ()=0
virtual nsapi_error_t disconnect ()=0
virtual nsapi_error_t gethostbyname (const char *host, SocketAddress *address, nsapi_version_t version=NSAPI_UNSPEC)
virtual nsapi_value_or_error_t gethostbyname_async (const char *host, hostbyname_cb_t callback, nsapi_version_t version=NSAPI_UNSPEC)
virtual nsapi_error_t gethostbyname_async_cancel (int id)
virtual nsapi_error_t add_dns_server (const SocketAddress &address)
virtual void attach (mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
virtual nsapi_connection_status_t get_connection_status () const
virtual nsapi_error_t set_blocking (bool blocking)
virtual WiFiInterfacewifiInterface ()
virtual MeshInterfacemeshInterface ()
virtual CellularBasecellularBase ()
virtual EMACInterfaceemacInterface ()
Static Public Member Functions
static EthInterfaceget_default_instance ()
 Static Public Member Functions inherited from NetworkInterface
static NetworkInterfaceget_default_instance ()
Static Protected Member Functions
static EthInterfaceget_target_default_instance ()
 Static Protected Member Functions inherited from NetworkInterface
static NetworkInterfaceget_target_default_instance ()
Additional Inherited Members
 Public Types inherited from NetworkInterface
typedef mbed::Callback< void(nsapi_error_t result, SocketAddress *address)> hostbyname_cb_t
 Public Types inherited from DNS
typedef mbed::Callback< void(nsapi_error_t result, SocketAddress *address)> hostbyname_cb_t
 Protected Member Functions inherited from NetworkInterface
virtual NetworkStackget_stack ()=0

EthInterface example

Here is an example of an HTTP client program. The program brings up Ethernet as the underlying network interface and uses it to perform an HTTP transaction over a TCPSocket:

#include "mbed.h"
#include "EthernetInterface.h"

// Network interface
EthernetInterface net;

// Socket demo
int main() {
    // Bring up the ethernet interface
    printf("Ethernet socket example\n");

    // Show the network address
    const char *ip = net.get_ip_address();
    printf("IP address is: %s\n", ip ? ip : "No IP");

    // Open a socket on the network interface, and create a TCP connection to mbed.org
    TCPSocket socket;
    socket.connect("developer.mbed.org", 80);

    // Send a simple http request
    char sbuffer[] = "GET / HTTP/1.1\r\nHost: developer.mbed.org\r\n\r\n";
    int scount = socket.send(sbuffer, sizeof sbuffer);
    printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);

    // Recieve a simple http response and print out the response line
    char rbuffer[64];
    int rcount = socket.recv(rbuffer, sizeof rbuffer);
    printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);

    // Close the socket to return its memory and bring down the network interface

    // Bring down the ethernet interface

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.