Forum Moderators: coopster
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.
Maybe take a look through the Stream Functions [php.net]
you could also try sending a newline
do you control the actual stream?
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!
[php.net...]
<?php
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}
?>
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]
$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]
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
I also saw something about 'making it return immediately'
ah, this was it
[php.net...]