Пример #1
0
bool UDPSocket::confEndpoint(Endpoint & ep)
{
    char * host;
    char cmd[30];
    if (!endpoint_configured) {
        host = ep.get_address();
        if (host[0] != '\0') {
            // set host
            sprintf(cmd, "set i h %s\r", host);
            if (!wifi->sendCommand(cmd, "AOK"))
                return false;
                
            // set remote port
            sprintf(cmd, "set i r %d\r", ep.get_port());
            if (!wifi->sendCommand(cmd, "AOK"))
                return false;
                
            wifi->exit();
            endpoint_configured = true;
            return true;
        }
    }
    return true;
}
Пример #2
0
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;
}