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

Ethernet

The EthInterface provides a C++ API for connecting to the internet over Ethernet.

EthInterface class reference

Additional Inherited Members
 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_error_t add_dns_server (const SocketAddress &address)
 Protected Member Functions inherited from NetworkInterface
virtual NetworkStackget_stack ()=0

Usage

To bring up the network interface:

  1. Instantiate the EthInterface class.
  2. Call the connect function.
  3. Once you connect the EthInterface, you can use it as a target for opening network sockets.

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");
    net.connect();

    // 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.open(&net);
    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
    socket.close();

    // Bring down the ethernet interface
    net.disconnect();
    printf("Done\n");
}

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.