Jump to content

Incomplete Socket data at destination


gcornelisse

Recommended Posts

I've setup a simple stream_socket_client and socket server scripts.  If I run the server script on a different machine than the client and send data, not all the data reaches the server.  If I run the server and client on the same machine there is no problem.  If I play around with the size of the chunks I'm sending I get slightly different results, but it never resolves the problem entirely.

 

I'd normally consider this a network issue, but I've seen this work on a PHP 4.3.x box as the client and a server somewhere else on the Internet.

 

Are there factors that impact how much data stream_socket_client can send?  I've checked my php.ini's to make sure there is plenty of memory available and timeouts aren't too short.

 

Any thoughts?  Thanks

 

Client:

#!/usr/bin/php -q
<?php

$fp = stream_socket_client("xxx.xxx.xxx.xxx:6016", $errno, $errstr, 30);

if ($fp)
{
$xml = file_get_contents('/data/bin/the.xml');

stream_set_blocking($fp, false);

$parts = str_split($xml, 128);

foreach ($parts as $chunk)
{
	fwrite($fp, $chunk);
}

$result = fwrite($fp, "\r\n");

fclose($fp);
}

 

Server:

#!/usr/bin/php -q
<?php

error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();

$address = 'xxx.xxx.xxx.xxx';
$port = 6016;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

do {
if (($msgsock = socket_accept($sock)) === false) {
	echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
	break;
}

do {
	if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
		echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
		break 2;
	}
	if (!$buf = trim($buf)) {
		continue;
	}
	if ($buf == 'quit') {
		break;
	}
	if ($buf == 'shutdown') {
		socket_close($msgsock);
		break 2;
	}

	file_put_contents('/data/bin/output.xml', $buf, FILE_APPEND);

	echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);

socket_close($sock);

Link to comment
https://forums.phpfreaks.com/topic/119702-incomplete-socket-data-at-destination/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.