Page 1 of 1

fsockopen not closing an open connection correctly?

Posted: Sun Jun 04, 2006 10:37 am
by Chris Corbyn
I'm seeing some strange behaviour with fsockopen().

I'm doing something like this:

Code: Select all

$sock = @fsockopen($host, $port, $errno, $errstr, $timeout);

$foo =& $sock;

fwrite($foo, $some_command); //Using reference

fgets($foo); //Using reference

fwrite($foo, $some_command);

// ** Not reading the response here because there sometimes isn't one **

fclose($sock); //Not by reference

$sock = false; //Just for safe measure

//Reconnect
$sock = @fsockopen($host, $port, $errno, $errstr, $timeout);

$foo =& $sock;

fwrite($foo, $some_command); //Using reference

fgets($foo); //Using reference

//It's reading data from the previous session wrongly?!!!
Basically, if I open a connection, send commands, then close it and open it again there's still data sitting in the socket which gets read wrongly when I'm using what I would expect to be a clean connection.

I need to be able to completely destroy the socket and create a completely clean/fresh start again but using the same variable names and references.

Is it my reference that's causing the problem? I need the reference since it gets used in several places and depending upon the method we use to connect the references may point to different things (too much to explain those reasons in the scope of this thread).

Any clues? The connection definitely closes, I've made sure of that because I get errors if I tgry to read while it's closed. But when it opens again something weird is happening.

Posted: Sun Jun 04, 2006 10:39 am
by Chris Corbyn
Oh yeah, I've tried setting The reference to false too... same issues. I've also tried using unset() to no avail :( Seems like some sort of PHP memory issue.

Posted: Sun Jun 04, 2006 2:35 pm
by Chris Corbyn
OK ignore me.... this doesn't seem to actually be the case with a pure procedural script like this where everything is global.

Code: Select all

<?php

$socket = fsockopen("smtp", 25, $errno, $errstr, 30);

$ref =& $socket;

while(1)
{
	echo $line = fgets($ref);
	if (preg_match('/\d+\ /', $line)) break;
}

fwrite($ref, "EHLO myhost\r\n");

fclose($socket);

$socket = false;
$ref = false;

$socket = fsockopen("smtp", 25, $errno, $errstr, 30);

$ref =& $socket;

while(1)
{
	echo $line = fgets($ref);
	if (preg_match('/\d+\ /', $line)) break;
}

fclose($socket);

?>
The connection has no left over data from the EHLO command here... this is what I expect. Must be something in my logic. There are a lot of references in my OO code (references to items inside objects too) so I think I must have a logic problem.

Posted: Sun Jun 04, 2006 2:45 pm
by Christopher
Probably unrelated, but $sock = null; is the equivalent of unset($sock);. I think $sock = false; will leave the reference count as it was.

Posted: Sun Jun 04, 2006 4:19 pm
by Chris Corbyn
arborint wrote:Probably unrelated, but $sock = null; is the equivalent of unset($sock);. I think $sock = false; will leave the reference count as it was.
Thanks :) Yeah I tried null among other things. The problem was actually something stupid. I was reading data from something I'd copied before disconnecting :oops: All's working nicely now :)

Posted: Sun Jun 04, 2006 4:39 pm
by Christopher
d11wtq wrote:The problem was actually something stupid.
Strange ... but I have that exact same bug all the time! :)

Posted: Sun Jun 04, 2006 4:50 pm
by Chris Corbyn
arborint wrote:
d11wtq wrote:The problem was actually something stupid.
Strange ... but I have that exact same bug all the time! :)
This is why I need to start unit testing... I downloaded simpletest last night and started reading the docs but it sent me to sleep :P (Only kidding, I'd been drinking).

I'm not too sure how easy it's going to be to test Swift without tearing parts of the code apart or adding in oodles more code to allow access to items that are private.

I'll no doubt be asking in our testing board though :) Need to finish reading the docs first.