0
std::array<char, 256> buffer;

while (1) { // top loop
    while (1) { // inner loop
        n = recv(sockfd, &buffer[0], buffer.size() - 1, 0);
        if (n == 0) {
            break;
        }
        memset(buffer.data(), 0, 256);
    }
    n = send(sockfd, "33", 3, MSG_NOSIGNAL);
}

I think that in one by one top loop, "recv" is blocked without "send". But In this code, first "recv" is blocked, but the infinite non-blocking loop occurs after "recv" is worked by one "send". I don't any "send" except for the first "send". In infinite loop, buffer size is 0 and, "n" is 0. I don't know why...

3088 K
  • 75
  • 5
  • This isn't C but C++. – Lundin Nov 15 '22 at 10:35
  • Could you please verify what return code you are getting for second call of recv ? – Vishal Nov 15 '22 at 10:58
  • Also, please verify whether you have set blocking mode for your socket. – Vishal Nov 15 '22 at 11:03
  • oops, I forgot the "send" line. I think this problem occurs. In "send" with MSG_NOSIGNAL, strerror(errno) is "Broken pipe". But I don't understand, why the "recv" is not blocked, before. There is no strerror(errno) for the second call of "recv". Mabye, in a loop, the blocking flag of "recv" is changed after first call of the "recv"? – 3088 K Nov 16 '22 at 01:48
  • I think, this is answer. https://stackoverflow.com/questions/61174580/c-recv-function-blocking-loop-from-repeating-after-receiving-everything-sys-soc But, I am a little ambiguous. – 3088 K Nov 16 '22 at 02:02
  • I find the definite answer. When other side sending the message, sends the close ack, the receiver receive it, and the "recv" return 0. So, the code do "break" in the inner loop, and it does top loop, again. That is, recv block is disappeared, when a receiver receives close ack. And always, the recv returns 0 without any blocks. Anything wrong? – 3088 K Nov 16 '22 at 02:46

0 Answers0