note 102249 modified in function.socket-write by danbrown

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Some clients (Flash's XMLSocket for example) won't fire a read event until a new line is recieved.

<?php
	/*
	 * Write to a socket
	 * add a newline and null character at the end
	 * some clients don't read until new line is recieved
	 *
	 * try to send the rest of the data if it gets truncated
	 */
	function write(&$sock,$msg) {
		$msg = "$msg\n\0";
		$length = strlen($msg);
		while(true) {
			$sent = socket_write($sock,$msg,$length);
			if($sent === false) {
				return false;
			}
			if($sent < $length) {
				$msg = substr($msg, $sent);
				$length -= $sent;
				print("Message truncated: Resending: $msg");
			} else {
				return true;
			}
		}
		return false;
	}
?>

--was--
Some clients (Flash's XMLSocket for example) won't fire a read event until a new line is recieved.

	/*
	 * Write to a socket
	 * add a newline and null character at the end
	 * some clients don't read until new line is recieved
	 *
	 * try to send the rest of the data if it gets truncated
	 */
	function write(&$sock,$msg) {
		$msg = "$msg\n\0";
		$length = strlen($msg);
		while(true) {
			$sent = socket_write($sock,$msg,$length);
			if($sent === false) {
				return false;
			}
			if($sent < $length) {
				$msg = substr($msg, $sent);
				$length -= $sent;
				print("Message truncated: Resending: $msg");
			} else {
				return true;
			}
		}
		return false;
	}

http://php.net/manual/en/function.socket-write.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.