For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > August 2004 > Net_Socket::readLine() problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Net_Socket::readLine() problem
Andreas Korthaus

2004-08-06, 8:56 am

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.php#391
http://cvs.php.net/co.php/pear/Net_.../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


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com