registering
12-08-2003, 01:23 PM
Hi All,
I've got a DB server that sends data like this:
HEADER\n
DATA\n
DATA\n
DATA\n
etc.
I read the header, then loop to read the data. However 100% of the time, the very first socket_read I call to read the data returns a newline with nothing, but every call after that works great. I've checked the server logs and am convinced it is not sending any superfluous newline between the HEADER and DATA.
So now I've got this ugly loop to just keep reading if I only get a single char back. The header is usually roughly 90 chars, each DATA is about 25 bytes:
Logger("TO SERVER:" . $msg, $debugFile);
if (socket_write($socket, $msg, strlen($msg)) == false)
{
Logger("Write failed: " . socket_strerror(socket_last_error($socket)), $debugFile);
} //end if
// Read header info
flush();
$data = "";
$data = socket_read($socket, 512, PHP_NORMAL_READ);
if ($data == false)
{
fclose($fileHandle);
Logger("Could not read from socket: " . socket_strerror(socket_last_error($socket)), $debugFile);
exit("Unable to read from socket\n");
} //end if
................................
//try to keep reading until we get valid data
do
{
$data = socket_read($socket, 100, PHP_NORMAL_READ);
if ($data === false)
{
Logger("data:" . $data . "\nREAD FAILED! " . socket_strerror(socket_last_error($socket)), $debugFile);
} //end if
if (strlen($data) == 1)
{
Logger("data:" . $data . "\nREAD FAILED with 1 byte read! " . socket_strerror(socket_last_error($socket)), $debugFile);
} //end if
} while (strlen($data) == 1);
Logger("data:" . $data, $debugFile);
sscanf($data, "%d %f", $timestamp, $raw_value);
Any ideas why that extra newline is there? Any tricks to socket_read??
The actual msg strerror prints out is:
Unknown error 1036284762
where the number 103whatever usually changes....:(
I've got a DB server that sends data like this:
HEADER\n
DATA\n
DATA\n
DATA\n
etc.
I read the header, then loop to read the data. However 100% of the time, the very first socket_read I call to read the data returns a newline with nothing, but every call after that works great. I've checked the server logs and am convinced it is not sending any superfluous newline between the HEADER and DATA.
So now I've got this ugly loop to just keep reading if I only get a single char back. The header is usually roughly 90 chars, each DATA is about 25 bytes:
Logger("TO SERVER:" . $msg, $debugFile);
if (socket_write($socket, $msg, strlen($msg)) == false)
{
Logger("Write failed: " . socket_strerror(socket_last_error($socket)), $debugFile);
} //end if
// Read header info
flush();
$data = "";
$data = socket_read($socket, 512, PHP_NORMAL_READ);
if ($data == false)
{
fclose($fileHandle);
Logger("Could not read from socket: " . socket_strerror(socket_last_error($socket)), $debugFile);
exit("Unable to read from socket\n");
} //end if
................................
//try to keep reading until we get valid data
do
{
$data = socket_read($socket, 100, PHP_NORMAL_READ);
if ($data === false)
{
Logger("data:" . $data . "\nREAD FAILED! " . socket_strerror(socket_last_error($socket)), $debugFile);
} //end if
if (strlen($data) == 1)
{
Logger("data:" . $data . "\nREAD FAILED with 1 byte read! " . socket_strerror(socket_last_error($socket)), $debugFile);
} //end if
} while (strlen($data) == 1);
Logger("data:" . $data, $debugFile);
sscanf($data, "%d %f", $timestamp, $raw_value);
Any ideas why that extra newline is there? Any tricks to socket_read??
The actual msg strerror prints out is:
Unknown error 1036284762
where the number 103whatever usually changes....:(