We help IT Professionals succeed at work.
Get Started

A call to function fsockopen to read the binary output of a CGI program yields packets duplication

Last Modified: 2013-12-25

I'm trying to download the binary output of a CGI program from a PHP script using 'fsockopen'. My problem is that rather than receiving the proper sequence of binary data, I get the sequence by chunk, and I get some chunks several times. And this duplication happens in a random fashion, i.e. I never get the same output. (but the CGI always sends the same binary data (for the sake of testing)). Also, each chunk seems to be preceded by its size (written in HEX format using ASCII) and a CRLF.

e.g. If the CGI outputs a binary sequence of 256 bytes containing the sequence (0,1,2,3,4,5,...., 255), then my output could be like (61,13,10,0,1,2,3,4,5,6,7,8,9,13,10,...., 255,13,10,13,10) where 61 is ASCII A and A is HEXA for 10 and 10 is the size of the following packet. 13,10 are CRLFs.

There does not seem to be any byte remapping. So this is not about the encoding. It's really a matter of big chucks of data being sent several times.

See the PHP code I have below.

What I tried so far:
- Downloading the binary just by executing my CGI from a browser: it works like a charm.
- Downloading a binary file using the PHP script below: works like a charm.
- Downloading from the CGI output using the stream_get_contents() function: yields the same problem as described above (duplication + packet size)

Thanks for your help.
$ServerURL = '';
$url = parse_url($ServerURL);
// Eventually, this will be a more complex POST request
$request = "GET " . $url['path'] . " HTTP/1.1\n";
$request .= "Connection: Close\n";
$request .= 'Host: ' . $url['host'] . "\n\n";
// Eventually, "" will be different so cannot assume it's local
if ($socket = fsockopen("", 80, $err_num, $err_msg, 5))
	fwrite($socket, $request);
	$buffer = '';
	while (!feof($socket))
		// print for testing purposes only
		print $buffer . "\n";
		$buffer .= fread($socket, 128);
	if ($houtput = fopen($BufferFileName, "wb"))
		fwrite($houtput, $buffer);

Open in new window

Watch Question
Top Expert 2008
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE