Jump to content

fsockopen () Cut Off


magicink

Recommended Posts

Hello,

 

I created a class that I use to open socket connections. I can connect to a target just fine and I can retrieve data. The real problem is that I don't seem to be able to acquire all the data when the response is longer than 600 or so characters long.

 

600 is a rough estimate because due to the fact that when I reload the page, sometimes I can acquire more data, sometimes I acquire less ~3 characters.

 

I figured the variant meant that there was an issue with the script timing out before all the data was acquired, but even if I set stream_set_timeout () to 10 minutes, it doesn't change the result.

 

Here is the class:

<?php

/**
 * SocketConnection manages a connection that is based upon the PHP
 * fopensock function. This code is based off of code originally
 * writen by Antone Roundy.
 * 
 * @author
 * @version 1.1
 */
class SocketConnection extends Connection
{
	private $connectionAddress;
	private $port;

	/**
	 * Constructs a new socket connection.
	 */
	public function SocketConnection ( $connectionAddress, $port )
	{
		parent::__construct ();
		$this->connectionAddress = $connectionAddress;
		$this->port = $port;
	}

	/**
	 * This methods attempts to make a connection to the socket.
	 */
	public function connect ()
	{
		$connectionErrors = parent::getConnectionErrors ();

		// If the wrong version of PHP is running...
		if ( ! PHPUtility::isValidPHPVersion ( '5.0.0' ) )
		{
			// Add version error.
			$connectionErrors->addError ( new PHPVersionError () );
		}

		// If no connection address has been provided...
		if ( ! strlen ( $this->connectionAddress ) )
		{
			// Add null connection address error.
			$connectionErrors->addError ( new NullConnectionAddressError () );
		}

		// If there are no errors...
		if ( $connectionErrors->getTotalErrors () == 0 )
		{
			// Attempt to establish a connection.
			$connectionHandler = @fsockopen ( $this->connectionAddress, $this->port, $errorNumber, $errorString );

			// If a connection has been established...
			if ( $connectionHandler )
			{
				// Store the connection.
				parent::setConnection ( $connectionHandler );

				stream_set_timeout ( parent::getConnection (), 0 );
			}
			// If a connection has not been established...
			else
			{
				// Add the error generated by the system.
				parent::getConnectionErrors ()->addError ( new ConnectionError ( $errorString ) );
			}
		}
	}

	/**
	 * This methods attempts to severe the connection.
	 */
	public function disconnect ()
	{
		$connection = parent::getConnection ();

		// If a connection exist...
		if ( $connection )
		{
			// Kill the connection.
			fclose ( $connection );

			parent::setConnection ( null );
		}
	}

	/**
	 * This method reads the socket stream buffer for any unread
	 * data and grabs it.
	 */
	public function getResponse ()
	{	
		$connection = parent::getConnection ();

		// Make sure that a connection exist
		if ( $connection )
		{
			parent::clearBuffer ();
			$response = '';

			// Cycle through the connection data atleast once.
			do
			{
				$response .= fread ( $connection, 8192 );
				$socketStatus = socket_get_status ( $connection );
			}
			while ( $socketStatus [ 'unread_bytes' ] );

			$response = trim ( preg_replace( "/^.*?\n(.*)\n[^\n]*$/", "$1", $response ) );

			parent::setBuffer ( $response );
		}
	}

	/**
	 * This method sends a command to the connection and records the response
	 * in the buffer.
	 */
	public function sendCommand ( $command )
	{
		$connection = parent::getConnection ();

		if ( $connection )
		{
			fputs ( $connection, $command . "\r" );
			sleep ( 1 );
			$this->getResponse ();
		}
	}
}

?>

 

The method responsible for acquiring data is SocketConnection::getResponse ().

 

The following is what I get when I create a SocketConnection and connect to a system.

 

getcc
<CalibrationCoefficients DeviceType = 'SBE37SI-RS232' SerialNumber = '03706087'>
<Calibration format = 'TEMP1' id = 'Temperature'>
<SerialNum>03706087</SerialNum>
<CalDate>04-Aug-08</CalDate>
<A0>7.668374e-05</A0>
<A1>2.643877e-04</A1>
<A2>-1.718200e-06</A2>
<A3>1.318193e-07</A3>
</Calibration>
<Calibration format = 'WBCOND0' id = 'Conductivity'>
<SerialNum>03706087</SerialNum>
<CalDate>04-Aug-08 </CalDate>
<G>-9.758729e-01</G>
<H>1.390257e-01</H>
<I>-2.276137e-04</I>
<J>3.585628e-05</J>
<P

 

The following is what I suppose to have:

 

<CalibrationCoefficients DeviceType = 'SBE37SI-RS232' SerialNumber = '03706087'>
   <Calibration format = 'TEMP1' id = 'Temperature'>
      <SerialNum>03706087</SerialNum>
      <CalDate>04-Aug-08</CalDate>
      <A0>7.668374e-05</A0>
      <A1>2.643877e-04</A1>
      <A2>-1.718200e-06</A2>
      <A3>1.318193e-07</A3>
   </Calibration>
   <Calibration format = 'WBCOND0' id = 'Conductivity'>
      <SerialNum>03706087</SerialNum>
      <CalDate>04-Aug-08 </CalDate>
      <G>-9.758729e-01</G>
      <H>1.390257e-01</H>
      <I>-2.276137e-04</I>
      <J>3.585628e-05</J>
      <PCOR>-9.570000e-08</PCOR>
      <TCOR>3.250000e-06</TCOR>
      <WBOTC>2.512962e-07</WBOTC>
      <REFERENCE_PRESSURE>0.000000e+00</REFERENCE_PRESSURE>
   </Calibration>
</CalibrationCoefficients>

 

Does anyone have any ideas as to why my content is being cut off?

Link to comment
https://forums.phpfreaks.com/topic/125561-fsockopen-cut-off/
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.