2

My query is related to “SO_RCVBUF” option in setsocketopt() api. When the new value specified for SO_RCVBUF in setsockopt() will take effect?

I am testing the flow control of TCP/IP and below is my environment. The client program is a slow reader and it has a single socket. To test the flow control, I am reducing the value of SO_RCVBUF to 5000 bytes after establishing the connection and before receiving the data. My expectation is that, my server program after sending 5000+ bytes should wait for client to read. But the server program sends the data till ~60000 bytes. After that the server program will wait for client to read. Once the client reads ~55000 bytes, then I see the new value of receive buffer (5000bytes) getting utilized.

Is it expected? I guess effect of setsocketopt() should be immediate. The initial value of receive buffer should not have any effect after setsocketopt().

Regards, Prashanth

alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    Note that the other end has a send buffer that will fill up too, which you might want to adjust as well. – nos Apr 28 '14 at 12:25

1 Answers1

2

I think you should set SO_RCVBUF before calling connect(), because that is when the buffers are allocated. By calling it afterward it may have no effect at all, or a delayed effect as you observed.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Thanks for the reply. My requirement was to modify the receive buffer size dynamically after connect. Is there any way. I want to understand how it works. Regards, Prashanth – user3581289 Apr 28 '14 at 12:43
  • Well yes you can modify it but as you already discovered it does not take effect instantly. I'm not aware of any way to make it take effect "faster" other than reconnecting or perhaps sending and receiving unnecessary data. I'm not really following why you would have this "requirement" in the first place? – John Zwinck Apr 28 '14 at 12:46
  • I have couple of sockets in our environment which are basically slow readers. I will not know about these sockets before connections. I will only know after they start reading. Once I identify them, I want to change their socket buffer size so that they will not occupy more memory. Regards, Prashanth – user3581289 Apr 28 '14 at 13:04
  • OK, so how about changing the buffer size to be small before you connect and then enlarging it later if you decide it's necessary? – John Zwinck Apr 28 '14 at 13:09
  • With that I might affect the good and genuine readers. My requirement is to restrict memory usage for the slow readers. Regards, Prashanth – user3581289 Apr 29 '14 at 04:10
  • I guess I don't quite see what you're getting at. How does setting your receive buffer size adversely affect other readers? Your receive buffer is for your reading, not for others reading. And if you really believe the slow readers are not "genuine," you should simple disconnect them. – John Zwinck Apr 29 '14 at 12:35