fsockopen not closing an open connection correctly?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

fsockopen not closing an open connection correctly?

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

d11wtq wrote:The problem was actually something stupid.
Strange ... but I have that exact same bug all the time! :)
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply