How to turn off buffering on write() system call?

后端 未结 1 1469
日久生厌
日久生厌 2021-01-06 11:24

I used to think that write() system call is unbuffered and that fwrite and fread are used for buffered IO. However I wrote simple prog

相关标签:
1条回答
  • 2021-01-06 11:35

    Perhaps what you mean is that you want to disable Nagle's Algorithm in which case the solution is:

    setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (int[]){1}, sizeof(int));
    

    Edit: Hmm, it looks like you want more than this, and I doubt what you want is possible without designing your own protocol on top of UDP.

    Edit 2: You may be able to get an effect similar to what you want by limiting the send and receive buffer sizes. The server (sender) should do:

    setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (int[]){YOUR_BUF_LIMIT}, sizeof(int));
    

    and the client (receiver) should do:

    setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (int[]){YOUR_BUF_LIMIT}, sizeof(int));
    
    0 讨论(0)
提交回复
热议问题