Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

N*(connect + send + close) vs (Nagle disable + connect + N*send + close) , N > 1

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:

  1. connect - send- close per message
  2. 1 connect - send multiple times and do 1 close at the end with the NA disabled. While I read the consequences of disabling the NA, It seems to me that opening and closing a socket every time just to send a message is an expensive price to pay as well.

Are there other solutions without leaving sockets?

Thanks.

like image 490
Armando Avatar asked Jun 02 '11 23:06

Armando


People also ask

When should I disable Nagle algorithm?

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.

What is Nagle's algorithm what problem does it aim to solve and how?

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.


1 Answers

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.

like image 144
Nemo Avatar answered Sep 18 '22 21:09

Nemo