0
votes

I want to use non-blocking API of recv but it doesn't work after 64KB data and give error: Resource temporarily unavailable. So I use if(error == EAGAIN) but it stuck on receive because no data is available.

while(true) {
        ret = recv(csd, buf, size, MSG_DONTWAIT);

        if(errno == EAGAIN) {
            continue;
        }

        if (ret < 0) {
            perror("Error in receive\n");
            close(csd);
            exit(EXIT_FAILURE);
        } else if (ret == 0) {
            fprintf(stderr, "client disconnected\n");
            close(csd);
        } else {
            return buf;
        }
    }
1
You should use select() instead of spin looping, or indeed use blocking mode, as you don't have anything else to do in between. Try a smaller receive buffer.user207421
I am using select() in my code, this is just a sample code I put here just to illustrate my issue .I don't wan't to use small buffer.Adil Siddiqui
So what exactly does 'after 64KB data' mean?user207421
I am sending and receiving data from 1 byte to 200 KB(in loop) and I want to send and receive this data from non-blocking API(send and receive). My send works fine with MSG_DONTWAIT but receive fails after receiving 64 KB bytes, my recv fails anytime after receiving above 64 KB data.Adil Siddiqui

1 Answers

2
votes

By default the socket uses a 64k buffer internally and then the kernel refuses to accept more data. So recv() can return at most 64kb of data without waiting.

You could change the buffer size for the socket (man 7 socket, SO_RCVBUF) or use a loop around select and recv to read it in multiple goes into a larger buffer as it becomes available.