Is it possible to use the `recvfrom` function to check if any previous data has been sent to the socket? Or need to actively listen?

I have a loop while(1)that uses recvfromto receive data that was sent to a domain socket from another process (P2).

The while loop should do 2 things, first listen for incoming data from P2, and secondly, run another function checkVoltage().

So this is a bit like this:

while(true)
{
    listenOnSocket() /*listens for 100 u seconds*/
    checkVoltage();
}

My problem is this: a function listenOnSocket()uses a function recvfromto check input from another process. He conducts listening for 100 seconds, then turns off and proceeds to perform the function checkVoltage(). Thus, it spends 99% of the time in function listenOnSocket(). My problem is that if P2 sends the information to the socket during a function checkVoltage(), then it will lead to error, stating: sending datagram message: No such file or directory.

Is there a way to check this loop for any data that was sent to the socket earlier? Thus, if P2 sends data during a function checkVoltage(), this will not result in an error.

Thank.

EDIT:

, listenOnSocket() FireControl, P1 (, P2), FireControl , . P2 P1 , , .

, , recvfrom, , , - .

, !

EDIT2: listenOnSocket():

command listenOnSocket(int timeout, float utimeout) /*Returns null payload when no input is detected*/
{
    command payload;
    int sock;
    socklen_t* length;
    struct sockaddr_un name;
    char buf[1024];

    struct timeval tv;
    tv.tv_sec = timeout;
    tv.tv_usec = utimeout;

    /* Create socket from which to read. */
    sock = socket(AF_UNIX, SOCK_DGRAM, 0);
    if (sock < 0) 
    {
        perror("opening datagram socket");
        payload = nullPayload;
    }

    /* Create name. */
    name.sun_family = AF_UNIX;
    strcpy(name.sun_path, NAME);

    unlink(name.sun_path);

    /* Bind the UNIX domain address to the created socket */
    if (bind(sock, (struct sockaddr *) &name, sizeof(struct sockaddr_un))) 
    {
        perror("binding name to datagram socket\n");
        payload = nullPayload;
    }

    /*Socket has been created at NAME*/
    if (timeout != 0 || utimeout != 0)
    {
        setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
    }
    else
    {
        tv.tv_sec = 0;
        tv.tv_usec = 0;
        setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
    }
    /* Read from the socket */
    if (recvfrom(sock, &payload, sizeof(command), 0, (struct sockaddr *)&name, &length) < 0) /*Less than zero results from a timeout*/
    {
        payload = nullPayload;
    }

    unlink(NAME);   
    return payload;
}

, :

while (1)
    {
        buffer = getADCValue();

        checkVoltage();

        temp = listenOnSocket(0, 100); /*Look for a new command*/
        doStuffWithTempIfItHasChanged();
        }
    }
+4
2

, , , recvfrom. , , .

+1

, , recvfrom, , ,

. listenOnSocket(), () - , , , (b) , , ... . .

+2

Source: https://habr.com/ru/post/1657437/


All Articles