9 years, 4 months ago.

Is there a way to check for data present in EthernetInterface UDPsocket?

I am using the Freescale K64F. I would like to use the EthernetInterface UDPsocket for Ethernet comms.

I want to be able to check if data is present at the socket, and have the program continue. If I use 'udps.receiveFrom(client, inbuf, sizeof(inbuf))' the program halts until there is some data to be received.

I see that the old Ethernet class in mbed has a function receive() which returns 0 if no data is present, and a separate read() function. How can I do this in EthernetInterface?

Thanks... David Jeffrey

1 Answer

9 years, 4 months ago.

Hi,

socket provides set_blocking() method, which should doi the trick

This is the implementation of receiveFrom() for UDP socket

int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
    if (_sock_fd < 0)
        return -1;
    
    if (!_blocking) {
        TimeInterval timeout(_timeout);
        if (wait_readable(timeout) != 0)
            return 0;
    }
    remote.reset_address();
    socklen_t remoteHostLen = sizeof(remote._remoteHost);
    return lwip_recvfrom(_sock_fd, buffer, length, 0, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
}

Accepted Answer

Thanks, Martin - I hadn't realised that methods from socket, as well as UDPsocket, could be used. Perfect answer.

posted by David Jeffrey 10 Dec 2014