开发者

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

开发者 https://www.devze.com 2023-03-09 22:44 出处:网络
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 se

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 under开发者_C百科stand. 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.


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.


@Nemo has given great advice for TCP.

But I suggest you look at UDP instead. TCP can introduce arbitrary delay during packet loss, and "TCP fairness" works based on forcing packet loss to occur. It's not ideal for low-latency transfers. Wanting to disable Nagle is a strong sign you're using the wrong protocol.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号