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...
Thanks,
Mark
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