UDP Echo Client example

Dependencies:   EthernetInterface mbed-rtos mbed

Deprecated

This is an mbed 2 networking example. For mbed OS 5, the networking libraries have been revised to better support additional network stacks and thread safety here.

main.cpp

Committer:
emilmont
Date:
2012-07-26
Revision:
1:129986b437b1
Parent:
0:97e3476ef63e
Child:
2:3307c4a7c499

File content as of revision 1:129986b437b1:

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

const char* ECHO_SERVER_ADDRESS = "10.2.131.73";
const int ECHO_PORT = 7;

int main() {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    
    UDPSocket sock;
    sock.init();
    
    char out_buffer[] = "Hello World\n";
    UDPPacket out_packet(out_buffer, sizeof(out_buffer));
    out_packet.set_address(ECHO_SERVER_ADDRESS, ECHO_PORT);
    sock.sendTo(out_packet);
    
    char in_buffer[256];
    UDPPacket in_packet(in_buffer, sizeof(in_buffer));
    int n = sock.receiveFrom(in_packet);
    
    in_buffer[n] = '\0';
    printf("%s\n", in_buffer);
    
    sock.close();
    
    eth.disconnect();
    while(1) {}
}