Working with the networking stack

Legacy Networking Libraries

This page currently covers the 'legacy' networking libraries. A completely new library has been written, with significantly improved performance and reliability.

Getting Started

This is a simple example program. We asume there that your mbed is connected to the internet through an Ethernet network.

To get this first example running, you will need both EthernetNetIf and HTTPClient packages.

You can import these packages in the compiler as libraries using those links:

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"

EthernetNetIf eth; 
HTTPClient http;
  
int main() {

  printf("Start\n");

  printf("\r\nSetting up...\r\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("\r\nSetup OK\r\n");
  
  HTTPText txt;
  
  HTTPResult r = http.get("http://mbed.org/media/uploads/donatien/hello.txt", &txt);
  if(r==HTTP_OK)
  {
    printf("Result :\"%s\"\n", txt.gets()); 
  }
  else
  {
    printf("Error %d\n", r);
  }
  
  while(1)
  {
  
  }
  
  return 0;
  
}

This program will basically retrieve the content of the "hello.txt" file and display it.

You can also import this program directly into the compiler: http://mbed.org/users/donatien/programs/HTTPClientExample

Stack architecture

Layering

The stack is ordered in three layers:

  • The interface layer provides network functionality to the stack through device-specific drivers. For instance, the Ethernet interface correspond to this layer.
  • The API exposes sockets to the top-level components. It abstracts the implementation from the user.
  • The high level modules contain all application-level protocol clients and servers, such as HTTP, NTP, etc.

Process

The stack is designed to run cooperatively with other services in a monothreaded environment. To achieve that some time-critical routines are executed in interrupt context, but most of the code is called in user context.

To function properly the stack must be polled at a regular interval. The static method Net::poll() ensures that every component of the stack keeps running. So you don't need to care about which module you have to poll or not.

To be able to work cooperatively in a single-threaded environment, the stack uses an event/callback model. Most methods are non blocking and raise an event on completion.

However, to keep things simple, some top-level components (such as the HTTP Client) expose as well an equivalent blocking function that returns on completion (basically it just calls Net::poll() for you ;)).

Packaging

The stack is split into two kinds of packages, of which you can get both the source or a precompiled version:

  • The Services packages contain the high level modules of the stack (eg. HTTPClient)
  • The Interface+API packages (eg. Ethernet+API = EthernetNetIf) are specific to a particular interface and contain the API layer as well for ease of use




14 comments:

30 Oct 2010

where is the contents of the text file displayed?

30 Oct 2010

If you open the USB/serial port with a terminal program the text is displayed there

08 Nov 2010

"The stack is split into two kinds of packages, of which you can get both the source or a precompiled version:"

Where's the source code? When I import this into the compiler I only ever see the precompiled version.

08 Nov 2010

Hi Andy

There's a link from here to here!

The latest version of the source does not make use of the AHBSRAM RAM banks as it should - check out the thread here as well.

Regards Daniel

08 Nov 2010

Thanks Daniel :)

27 Jun 2011

HTTPText txt;

HTTPResult r = http.get("http://mbed.org/media/uploads/donatien/hello.txt", &txt); if(r==HTTP_OK) { printf("Result :\"%s\"\n", txt.gets());

this will give us ==> Result :"Hello World!"

if i wan only ==> Hello

how to make it?

06 Oct 2011

user Pier Juhng Hiah wrote:

HTTPText txt;

HTTPResult r = http.get("http://mbed.org/media/uploads/donatien/hello.txt", &txt); if(r==HTTP_OK) { printf("Result :\"%s\"\n", txt.gets());

this will give us ==> Result :"Hello World!"

if i wan only ==> Hello

how to make it?

Call mbed to change the text on their site. You have to be aware that when you see the text "Hello World" on your screen, it has been downloaded from the url you entered in your code.

24 Apr 2012

Hi!

I imported the HTTPClientExample project. I try to compile it, but i got 4 errors.

"cannot open source input file "api/socket.h": No such file or directory" in file "lib/HTTPClient/HTTPClient.h", Line: 31, Col: 23

"cannot open source input file "core/fwk.h": No such file or directory" in file "lib/HTTPClient/HTTPClient.cpp", Line: 29, Col: 21

"cannot open source input file "core/fwk.h": No such file or directory" in file "lib/HTTPClient/data/HTTPMap.cpp", Line: 24, Col: 21

"cannot open source input file "core/fwk.h": No such file or directory" in file "lib/HTTPClient/data/HTTPText.cpp", Line: 24, Col: 21

Could someone help me? Thanks!

30 Apr 2012

I'm having the exact same errors...

user Zoltan Somogyi wrote:

Hi!

I imported the HTTPClientExample project. I try to compile it, but i got 4 errors.

"cannot open source input file "api/socket.h": No such file or directory" in file "lib/HTTPClient/HTTPClient.h", Line: 31, Col: 23

"cannot open source input file "core/fwk.h": No such file or directory" in file "lib/HTTPClient/HTTPClient.cpp", Line: 29, Col: 21

"cannot open source input file "core/fwk.h": No such file or directory" in file "lib/HTTPClient/data/HTTPMap.cpp", Line: 24, Col: 21

"cannot open source input file "core/fwk.h": No such file or directory" in file "lib/HTTPClient/data/HTTPText.cpp", Line: 24, Col: 21

Could someone help me? Thanks!

02 May 2012

Me too!

user Nick Desaulniers wrote:

I'm having the exact same errors...

user Zoltan Somogyi wrote:

Hi!

I imported the HTTPClientExample project. I try to compile it, but i got 4 errors.

"cannot open source input file "api/socket.h": No such file or directory" in file "lib/HTTPClient/HTTPClient.h", Line: 31, Col: 23

"cannot open source input file "core/fwk.h": No such file or directory" in file "lib/HTTPClient/HTTPClient.cpp", Line: 29, Col: 21

"cannot open source input file "core/fwk.h": No such file or directory" in file "lib/HTTPClient/data/HTTPMap.cpp", Line: 24, Col: 21

"cannot open source input file "core/fwk.h": No such file or directory" in file "lib/HTTPClient/data/HTTPText.cpp", Line: 24, Col: 21

Could someone help me? Thanks!

03 May 2012

You can try this version that I just built with the online compiler by switching to the NetServices library.

[Not found]

03 May 2012

Dude, you rock!

Here's the link to the NetServices library: http://mbed.org/users/segundo/libraries/NetServices/ljhqix

user Adam Green wrote:

You can try this version that I just built with the online compiler by switching to the NetServices library.

[Not found]

09 May 2012

Hi - I;ve got the example code working and was looking at the api for the non-blocking version. Do you have an example ? I am fairly new to C++ and cannot figure out what I need for the parameters for the non blocking version ?

22 Feb 2013

I am getting the folowing error when I try to compile the code "http://mbed.org/users/donatien/programs/HTTPClientExample": "cannot open source input file "TCPSocketConnection.h": No such file or directory" in file "HTTPClientHTTPClient.h", Line: 27, Col: 32 Can anyone advise how to eliminate this error?