Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP socket_read

Status
Not open for further replies.

Kozusnik

Programmer
Feb 27, 2005
620
Hi all,
I've been looking for a good way to get around the escape characters in the socket_read function. I'm trying to allow a multiline message to be accepted by my server through a socket for an hl7 project. Lines are escaped by hex(0D) and the entire message is escaped by hex(0D0A). Unfortunately when socket read encounters a \n or \r, it considers that the end of the message. After several iterations of playing, I've moved back to base. It accepts 1 message and returns it back to the client (this is where I'll be sending back the ACK message). If anyone has sample code or another suggestion, I'd certainly appreciate it. Here's the base for the code...
Code:
#!/usr/bin/php -q
<?php
set_time_limit (0);

$host = "localhost";
$port = 1234;

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");

$input = "";
do{
$input = socket_read($spawn, 65536, 2) or die("Could not read input\n");

//Write ACK
if (trim($input) != ""){
socket_write($spawn, $input , strlen ($input)) or die("Could not write output\n");
}

}
while (true);
socket_close($socket);
?>

Thanks,
Mark
 
Unfortunately when socket read encounters a \n or \r, it considers that the end of the message

this should not be the case in recent versions of php. you can make the read binary safe by changing the type argument to PHP_BINARY_READ

otherwise i would wrap the retrieval in a while loop rather than a do while

Code:
while (false !== ($input = socket_read($spawn, 5536, PHP_BINARY_READ))){

// do stuff
}
 
Thanks jpadie, I'll try that in the morning.

I thought that the int 2 in the socket_read was the same as the BINARY_READ. I had set up a loop, iterated a variable and posted to stdout, but it seemed very inconsistent.

I'll let you know how it works.

Thanks again,
Mark
 
That seems to have fixed it. Once I changed to binary, it allows the whole message to post. I thought about a bin2hex and checking for 0d0a, but it seems unnecessary for what I'm doing right now. Here's the code as promised.

Thanks again,
Mark

Code:
#!/usr/bin/php -q
<?php
set_time_limit (0);

$host = "localhost";
$port = 1234;

$link = mysql_connect('localhost', 'username', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('interface') or die('Could not select database');

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");

while (false !== ($input = socket_read($spawn, 65536, PHP_BINARY_READ))){
$dtnow = date("Y-m-d H:i:s");
$pipe_parse = explode("|", $input);
$carat_msh9 = explode("^", $pipe_parse[8]);

$input = str_replace('\'','\'',  $input);
$input = str_replace('"','\\"',  $input);
$input = str_replace(',','\\,',  $input);

$query = "INSERT INTO in_msg (in_id, in_msg, in_event, in_event_type, in_datetime, in_read) VALUES(NULL, '$input', '$carat_msh9[0]', '$carat_msh9[1]', '$dtnow', '')";
$result = mysql_query($query) or die( mysql_error());

$output = "MSH|^~\&||$pipe_parse[3]|||$pipe_parse[6]||ACK|$pipe_parse[9]|$pipe_parse[10]|$pipe_parse[11]\rMSA|AA|$pipe_parse[9]";
socket_write($spawn, $output , strlen ($output)) or die("Could not write output\n");
}
socket_close($socket);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top