Search code examples
csocketsudpsend

How to turn this write_all in non blocking function to write bytes through UDP?


I have this function in C and I would like to use it to write bytes through my udp socket in non-blocking mode. I should complete the "TO BE DONE" part. Anyone could help me to understand what to do please? Does recalling the write function there could make any sense?

ssize_t nonblocking_write_all(int fd, const void *ptr, size_t n)
    {
        size_t n_left = n;
        while (n_left > 0) {
            ssize_t n_written = write(fd, ptr, n_left);
            if (n_written < 0) {
    
    /*** TO BE DONE START ***/
    
    
    /*** TO BE DONE END ***/
    
                if (n_left == n)
                    return -1; /* nothing has been written */
                else
                    break; /* we have written something */
            } else if (n_written == 0) {
                            continue;
            }
            n_left -= n_written;
            ptr += n_written;
        }
        assert(n - n_left >= 0);
        return n - n_left;
    }

Solution

  • This worked: n_written=nonblocking_write_all(fd,ptr,n_left);