Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: NetRelais TCP_Client_Example TCP_Server_Example UDP_Server_Example ... more
tcp client example
#include "mbed.h"
#include "EthernetInterface.h"
#include "NetworkAPI/buffer.hpp"
#include "NetworkAPI/ip/address.hpp"
#include "NetworkAPI/tcp/socket.hpp"
int
main()
{
EthernetInterface interface;
interface.init();
interface.connect();
printf("IP Address is %s\n\r", interface.getIPAddress());
int result;
network::tcp::Socket socket;
network::Buffer buffer(256);
std::string request("GET /media/uploads/donatien/hello.txt HTTP/1.1\r\nHost: %s\r\n\r\n");
if (socket.open() < 0) {
printf("Failed to open TCP Socket\n\r");
return -1;
}
if (socket.connect("mbed.org", 80) < 0) {
printf("Failed to connect with mbed.org\n\r");
return -1;
}
if (socket.write((void *)request.data(), request.size()) < 0) {
printf("Failed to write HTTP request\n\r");
return -1;
}
do
{
result = socket.read(buffer);
printf("Received %d bytes:\n\r%s\n\r", result, (char *)buffer.pointer());
} while(result > 0);
socket.close();
return 0;
}