Asterisk fwrite() returned error: Broken pipe

Hi guys, I’m currently having an issue in Asterisk 11 using AMI with PHP progamming. Below is the asterisk-cli error:

== Manager 'asterisk' logged on from 192.168.*.* [Oct 9 00:10:59] ERROR[14131]: utils.c:1234 ast_careful_fwrite: fwrite() returned error: Broken pipe [Oct 9 00:10:59] ERROR[14131]: utils.c:1234 ast_careful_fwrite: fwrite() returned error: Broken pipe [Oct 9 00:10:59] ERROR[14131]: utils.c:1234 ast_careful_fwrite: fwrite() returned error: Broken pipe == Manager 'asterisk' logged off from 192.168.*.*

And below is the PHP script to dial using AMI thru fsockopen():

[code]/* self::connect() contains :


if (!(self::$socket = @fsockopen($_SESSION[“pbx_server_connection”], “5038”, $err_no, $err_str, 4))):
self::$err = “Error connecting to PBX server: $err_str!”;
return false;
endif;
return true;


*/

if (!self::connect()) return -1;

	stream_set_timeout(self::$socket, 4);
	
	$channel = "";
	$cdr_link_id = "";
	
	$command = "Action: Login\r\n";
	$command .= "Username: ********\r\n";
	$command .= "Secret: ********\r\n\r\n";
	
	fwrite(self::$socket,$command);
	$phone_extension = $this->phone_ext($agent_extension);
	if (!empty($phone_extension)):
		$account_code = str_replace(".", "", microtime(true)) . mt_rand(10, 99);
		
		$command = "Action: originate\r\n";
		$command .= "Exten: $phone_number\r\n";
		$command .= "Context: LocalSets\r\n";
		$command .= "Channel: $phone_extension\r\n";
		$command .= "Account: $account_code\r\n";
		$command .= "Set: mydialid=$DID\r\n";
		$command .= "Priority: 1\r\n\r\n";
		//$command .= "Action: logoff\r\n\r\n";
		
		$response = "";
		fwrite(self::$socket,$command);
		do{
			$response .= fgets(self::$socket);
			//$response[] .= $line;
			$info = stream_get_meta_data(self::$socket);
		}while(!feof(self::$socket) && $info['timed_out'] == FALSE);
		
		preg_match("/^Channel:\s(\S+)\s*$/m", $response, $match);
		$channel = $match[1];
		preg_match("/^Uniqueid:\s(\S+)\s*$/m", $response, $match);
		$cdr_link_id = $match[1];
		return 0;
	endif;[/code]

I really don’t know what’s going on here, please, I need your brilliant ideas. Thanks!

You haven’t logged out. Also, did you disable read permissions for the account. If not, you will receive events.

The error happens when you close the TCP connection without first shutting down the manager session, so that Asterisk tries to send information after the connection has gone away.

Great! It works now. Thanks for sharing your thoughts Dav… :smile: