Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

TCP writes delayed

Started by spraff Dec 24, 2008 at 10:24 AM 1 replies 500+ views
Original Post
spraff
spraff
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!
Evil Steve
Evil Steve
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.
Telastyn
Telastyn
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.