The EthernetInterface provides a C++ API for connecting to the internet over an Ethernet connection.



To bring up the network interface:

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


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
    TCPSocket socket;;
    socket.connect("", 80);

    // Send a simple http request
    char sbuffer[] = "GET / HTTP/1.1\r\nHost:\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