Unix Socket Programming Question:
Download Job Interview Questions and Answers PDF
How do I use TCP_NODELAY?
Answer:
First off, be sure you really want to use it in the first place. It will disable the Nagle algorithm (see ``2.11 How can I force a socket to send the data in its buffer?''), which will cause network traffic to increase, with smaller than needed packets wasting bandwidth. Also, from what I have been able to tell, the speed increase is very small, so you should probably do it without TCP_NODELAY first, and only turn it on if there is a problem.
Here is a code example,
with a warning about using it
int flag = 1;
int result = setsockopt(sock,
/* socket affected */
IPPROTO_TCP,
/* set option at TCP level */
TCP_NODELAY,
/* name of option */
(char *) &flag,
/* the cast is historical cruft */
sizeof(int));
/* length of option value */
if (result < 0)
... handle the error ...
TCP_NODELAY is for a specific purpose; to disable the Nagle buffering algorithm. It should only be set for applications that send frequent small bursts of information without getting an immediate response, where timely delivery of data is required (the canonical example is mouse movements).
Here is a code example,
with a warning about using it
int flag = 1;
int result = setsockopt(sock,
/* socket affected */
IPPROTO_TCP,
/* set option at TCP level */
TCP_NODELAY,
/* name of option */
(char *) &flag,
/* the cast is historical cruft */
sizeof(int));
/* length of option value */
if (result < 0)
... handle the error ...
TCP_NODELAY is for a specific purpose; to disable the Nagle buffering algorithm. It should only be set for applications that send frequent small bursts of information without getting an immediate response, where timely delivery of data is required (the canonical example is mouse movements).
Download Unix Socket Programming Interview Questions And Answers
PDF
Previous Question | Next Question |
Whats the difference between select() and poll()? | What is the difference between read() and recv()? |