I'm new to socket programming (as you already figure out by my silly question), but keeping my shame aside, I'm writing a program using TCP posix. My constrain is the following: The message to be sent from the client to the server,should be read as byte stream and while my application is not high performance, the message should be deliver as soon as possible. I wrote a TCP client class with the intention of doing the following: 1 connect - many send - and 1 close at the end of the streaming. The problem is that the messages does not get deliver in near-real-time (I'm assuming its waiting to have a larger package for better throughput) After doing some research online, I found that while you can disable the Nagle algorithm (NA), it is s a very bad idea to do so. Since I'm new on socket programming, I don't want to disable features that I don't fully understand. So I'm left with two (bad?) options:
Are there other solutions without leaving sockets?
Thanks.
Nagle's algorithm should be disabled by enabling TCP_NODELAY by the requester in this case. If the response data can be larger than a packet, the responder should also disable Nagle's algorithm by enabling TCP_NODELAY so the requester can promptly receive the whole response.
Nagle's algorithm is a TCP optimization that makes the stack wait until all data is acknowledged on a connection before sending more data. This process, called "nagling", increases the efficiency of a network application system by decreasing the number of packets that must be sent.
In your case, disabling Nagle is exactly what you want to do.
Just remember that every call to write() is going to transmit your data immediately. So be sure you are packing your entire message together and then calling write() (or writev()) once when you are ready to send; do not call write() repeatedly with small payloads because that will be slow.
Situations like yours are exactly why they let you disable Nagle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With