Jump to content

fsockopen and streaming ASCII file


shakeitdown

Recommended Posts

hi, i use fsockopen to connect to a socket.  i use the following code to read an ASCII file (i got it straight off php.net)

 

// There may be a better way to connect to the stream than with fsockopen.  
public function connect()
{
	echo "Connecting to feed server...";
	$this->connection = fsockopen($this->ip, $this->port, $this->errno, $this->errstr, 30);
	if (!$this->connection) {
		echo $this->errstr." (".$this->errno.")\n";
	} else {
		echo "SUCCESS.\n\n";
	}
}

// THIS IS THE PROBLEM FUNCTION I THINK
// Simulates an un-buffered fgets so that stdin doesnt hang there waiting for some input 
// (i.e. it reads only if there is data available)
public function read()
{	
	$pArr = array($this->connection);

	if (false === ($num_changed_streams = stream_select($pArr, $write = NULL, $except = NULL, 0)))
	{
		print("\$ 001 Socket Error : UNABLE TO WATCH STDIN.\n");
	} elseif ($num_changed_streams > 0)
	{
			return explode(" ", str_replace("\n", "", trim(fgets($this->connection, 1024))));
	}
}		

 

what ends up happening is that every so often a line is NOT read.  it is totally skipped.  i am guessing that the server i'm connecting to is pumping out lines faster than i can call the read() function.

 

i am good at php but have never worked with sockets and streams.  i'm not really sure what the code above does or does wrong.  what i do know is that the stream looks something like this:

 

10020 MZ BASE 3920987 J 2831

10021 MZ BASQ 39201236 J 2326

10022 MZ BTRE 3543173 J 2428

// ...etc...

// doesn't end (no feof)

 

as far as i can tell there is no carriage return or line feed at the end (but there must be something hidden in there that signifies a line break, right?).  i also know there is no feof because the stream never ends.  i don't know how long the buffer is in bytes, because i don't know how to determine how many bytes the above line is.  what i want to do is obviously grab every line in the feed.  can ya help!???

Link to comment
https://forums.phpfreaks.com/topic/71424-fsockopen-and-streaming-ascii-file/
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.