Forum Moderators: coopster

Message Too Old, No Replies

PHP, Data stream no EOF - yet

         

Lvcien

1:21 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



A socket stream is opened with fsockopen() (possibly [pfsockopen() in the future]

A random amount of data is obtained from the stream with fgets()
From there the stream waits for data, this data will be sent to the stream using fwrite().

This fgets/fwrite cycle will repeated several times.

My question is this, How do I know when the stream stops sending data(no EOF is sent as this isn't the end) and is waiting for data to be sent to it.

jatar_k

1:45 pm on Feb 6, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Lvcien,

Maybe take a look through the Stream Functions [php.net]

you could also try sending a newline

do you control the actual stream?

Lvcien

3:09 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



No, the stream is not controlled.

Basically, you're connecting to a network device(switch, router, etc) with a raw socket. This is used with multiple devices so the input it recieves is different per device so I can't, for example read up to the > in router>, etc.

At a certain point, when the data stops streaming it waits for say a user name or a password to log into the device, Or perhaps a command to enter a privileged mode (ie: enable).

This is the idea I had but I can't figure out the positioning in the loop.

do
{ do
{
$data_rcvd .= fgets($connection, 128);

} while(strcasecmp($data_rcvd,$data_cmp) == 0);

$data_cmp .= $data_rcvd;

} while(strcasecmp($data_rcvd,$data_cmp) == 0);

Basically, this should loop while the two values do not match, as $data_rcvd has additional data appended onto each cycle EXECPT then no more data passes through, and at that point the other string $data_cmp WILL match, and then you know to pass the data to the device...

Does this sound correct?

Does while(strcasecmp($data_rcvd,$data_cmp) == 0) in this loop, loop currently when the match? or when they don't?

Thanks!

jatar_k

3:31 pm on Feb 6, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it would loop while they match

[php.net...]

<?php
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}
?>

Lvcien

4:24 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



I don't have the full source with me at the moment so I can't test it, but I think this will do it:

do
{
$data_cmp .= $data_rcvd;
$data_rcvd .= fgets($connection, 128);
} while(strcasecmp($data_rcvd,$data_cmp)!= 0);

For this example let's say that the data is pulled in two parts (ie: foo bar)

Do the loop:
Set $data_cmp equal to $data_rcvd which is currently nothing ("")
Get the data (foo)

$data_cmp = "";
$data_rcvd = foo;

Since the variable DO NOT match, repeat the loop

Append $data_rcvd to $data_cmp which is currently nothing still (""), which becomes $data_cmp = "foo"
Get the data (bar) and append it to $data_rcvd;

$data_cmp = "foo";
$data_rcvd = "foobar";

Again, the don't match - repeat the loop

Append $data_rcvd to $data_cmp which is currently "foo", which becomes $data_cmp = "foobar"
Get the data (""), now nothing as no more data is sent and append it to $data_rcvd, which is still "foobar"

$data_cmp = "foo";
$data_rcvd = "foobar"

Now the variables match so the loop is broken.

Does this sound correct?

Thanks!

[edited by: Lvcien at 4:27 pm (utc) on Feb. 6, 2007]

jatar_k

4:52 pm on Feb 6, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think you had a little typo in your last example as you meant

$data_cmp = "foobar";
$data_rcvd = "foobar"

but, yes, in theory and talking it through it does seem right

Lvcien

5:22 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



Excellent, I just pulled the source and I'll give it a shot and let you know.

Thanks.

Lvcien

9:07 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



This is the code


$data_cmp = ".";
do
{
if (strcasecmp($data_rcvd,$data_cmp)== 0)
{
break;
}
$data_cmp = $data_rcvd;
$data_rcvd .= fgets($connection, 128);
} while(strcasecmp($data_rcvd,$data_cmp)!= 0);

Fatal error: Maximum execution time of 60 seconds exceeded in

Times out at this line: $data_rcvd .= fgets($connection, 128);

Which I don't understand... is it really that slow?

edit:

Did a little more research on this... seems that $data_rcvd .= fgets($connection, 128); hangs when there is no data being sent until, data sends again - ie: it stops at "Enter Password:" After 30 seconds or so, this prompt times out and data is sent.

Maybe there is a function or some other way that can check a stream for data, before a fgets() is sent...

[edited by: Lvcien at 9:17 pm (utc) on Feb. 6, 2007]

jatar_k

9:17 pm on Feb 6, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



may not be in the first iteration

and it could be that slow, do you control all the machines you are working with?

you could play around with max_execution_time and see how long it actually takes.

hmm, this is one of those itmes I would love to have my good friend the admin and myself sitting playing around ;)

<after edit>

try looking through the stream functions to see if there is something, that's my first thought

Lvcien

10:30 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



I checked the stream functions and I can't find anything that would work...

edit:

Maybe something involving

[php.net...]

or

[php.net...]
[php.net...]

hmm...

[edited by: Lvcien at 10:40 pm (utc) on Feb. 6, 2007]

jatar_k

3:38 am on Feb 7, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the timeout was the one that caught my eye

I also saw something about 'making it return immediately'

ah, this was it
[php.net...]

Lvcien

2:25 pm on Feb 7, 2007 (gmt 0)

10+ Year Member



I noticed that last night but didn't have time to play with it...

There's a free script called PHPTelnet (google it) -- I may just hack that code up and see what it's doing.. I'll give this a shot.

Lvcien

3:21 pm on Feb 7, 2007 (gmt 0)

10+ Year Member



stream_set_blocking($connection,0);

seems to work, I'll have to work out a few bugs before my application works as needed. I'll post the corrected code here as soon as I get a chance.

Thanks!

jatar_k

3:25 pm on Feb 7, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sweet, thought it might but you never know how functions will work exactly though they sound good