Net_Socket::readLine() problem
| From: | Andreas Korthaus | Date: | Fri, 06 Aug 2004 08:27:57 +0000 |
| Subject: | Net_Socket::readLine() problem | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-13981@lists.php.net to get a copy of this message | ||
Hi!
I want to parse a response from a server with an own TCP-Protocol.
I have a response like that:
202 List follows\n
Count: 1\n
Content-Length: 5\n
\n
klaus
I want to parse this using Net_Socket::readLine(). Because readLine() should
read line by line, and strips \n, I thought I could do something like this:
$firstline=$socket->readLine();
while ($n=$socket->readLine()) {
if (!$n) {
break;
}
$line = explode(':',$n);
$params[rtrim($line[0])] = ltrim($line[1]);
}
$data = $socket->read($params['Content-Length']);
But this does not work, because readLine() does not return the line "\n" as
"", this \n is added to the next line, so readline only returns "\nklaus"
Here a more simple example with the same tcp-response:
var_dump($socket->readLine());
var_dump($socket->readLine());
var_dump($socket->readLine());
var_dump($socket->readLine());
This displays:
string(8) "Count: 1"
string(17) "Content-Length: 5"
string(6) "
klaus"
string(0) ""
But if I try this:
var_dump($socket->gets(32));
var_dump($socket->gets(32));
var_dump($socket->gets(32));
var_dump($socket->gets(32));
it displays:
string(9) "Count: 1
"
string(18) "Content-Length: 5
"
string(1) "
"
string(5) "klaus"
as I would expect it.
Also from looking into the source,
http://cvs.php.net/co.php/pear/Net_Socket/Socket.php#391
http://cvs.php.net/co.php/pear/Net_Socket/Socket.php#234
I do not understand why readLine() does not work as I expect.
Is this the desired behaviour of readLine()?
kind regards,
Andreas