Example #1
0
void udp_client_task(void const *argument)
{
    while (host_port == 0) {
        // Waiting for HOST port notification
    }

    DigitalOut indicator(LED2);
    UDPSocket socket;
    socket.init();

    Endpoint echo_server;
    echo_server.set_address(host_address, host_port);
    //printf("[udp_client_task] Start: %s:%d\r\n", host_address, host_port);

    while (1) {
        std::string datagram;
        bool sent_datagram = false;
        cli_serv_mutex.lock(); // LOCK
        if (datagram_queue.size() > 0) {
            // POP from datagram queue
            datagram = datagram_queue.back();
            datagram_queue.pop_back();
            sent_datagram = true;
        }
        cli_serv_mutex.unlock(); // LOCK
        if (sent_datagram) {
            //printf("[udp_client_task] Forwarded datagram: %s\r\n", datagram.c_str());
            socket.sendTo(echo_server, (char *)datagram.c_str(), datagram.length());
            forwarded_packets++;
            indicator = !indicator;
        }
    }
}
Example #2
0
void ethSetup()
{
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());

    udp.init();
    udp.bind(5683);

    udp.set_blocking(false, 10000);
}
Example #3
0
int main() {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    
    UDPSocket sock;
    sock.init();
    
    Endpoint multicast_group;
    multicast_group.set_address(MCAST_GRP, MCAST_PORT);
    
    char out_buffer[] = "very important data";
    while (true) {
        printf("Multicast to group: %s\n", MCAST_GRP);
        sock.sendTo(multicast_group, out_buffer, sizeof(out_buffer));
        Thread::wait(1000);
    }
}
Example #4
0
File: main.cpp Project: AsamQi/mbed
int main() {
    bool result = false;
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("UDP client IP Address is %s\n", eth.getIPAddress());

    UDPSocket sock;
    sock.init();

    Endpoint nist;
    nist.set_address(HTTP_SERVER_NAME, HTTP_SERVER_PORT);

    char out_buffer[] = "plop"; // Does not matter
    sock.sendTo(nist, out_buffer, sizeof(out_buffer));

    union {
        char in_buffer_tab[4];
        unsigned int in_buffer_uint;
    };

    const int n = sock.receiveFrom(nist, in_buffer_tab, sizeof(in_buffer_tab));
    if (n > 0) {
        result = true;
        const unsigned int timeRes = ntohl(in_buffer_uint);
        const float years = timeRes / 60.0 / 60.0 / 24.0 / 365;
        printf("UDP: Received %d bytes from server %s on port %d\r\n", n, nist.get_address(), nist.get_port());
        printf("UDP: %u seconds since 01/01/1900 00:00 GMT ... %s\r\n", timeRes, timeRes > 0 ? "[OK]" : "[FAIL]");
        printf("UDP: %.2f years since 01/01/1900 00:00 GMT ... %s\r\n", years, timeRes > YEARS_TO_PASS ? "[OK]" : "[FAIL]");

        if (years < YEARS_TO_PASS) {
            result = false;
        }
    }
    sock.close();
    eth.disconnect();
    notify_completion(result);
    return 0;
}