# TCP writes delayed

This topic is 3349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there. I have a service daemon running which listens for TCP/IP connections. It receives text commands and writes out text replies very quickly. My PHP script uses it like this:
	$socket = socket_create (AF_INET6, SOCK_STREAM, SOL_TCP); if (!$socket || !socket_connect ($socket,$address, $port)) return ""; echo "About to write.\n"; socket_write ($socket, $command, strlen($command));
echo "Written\n";
$p = socket_read ($socket, 100);

The connection is made immediately (the first echo executes) but there is a sometimes a long pause before the second echo. I understand that TCP/IP makes no guarantees on delivery time, and has no conecpt of a flush, but that doesn't explain why the function takes so long to return. Upwards of 20 seconds very often, and at the moment it's connecting to localhost. Any thoughts on why this might be happening? I'm certain the problem isn't at the daemon end. Thanks and Merry Christmas!

##### Share on other sites
I'd use a packet analyser like WireShark to see what's going on there. And I'd also connect to the web server with telnet to make sure that it's not just your browser caching the data for whatever reason. You enter telnet localhost 80 from the command prompt, then send:
GET / HTTP/1.0
Host: yourdomain.com

And then end with two newlines.

##### Share on other sites
Batching.

It's silly to write a packet for every 20 bytes of payload (the header is bigger than that!), so most things batch packets until it hits the MTU (usually 1500 bytes) and then sends the message. On most unix-likes I've seen, adding a newline to the end of the message is sufficient to cause it to flush for each message.