sockets - PHP to receive data being sent to a port -
I have a device that is sending data to a port on my local computer (for now, it is a site later ) The following code receives the data from the port using PHP. I have to enter 127.0.0.1/web/1.php (my filename) in the URL to work. This will be a problem when I'm alive after all, even when I get my data, the socket is closed.
Received from any advice, I have a VB background and PHP and Web is very new to me! Thanks
This is my current PHP
error_reporting (E_ALL); / * Allow hang around waiting for script connection * / Set_time_limit (0); / * Turn on the built-in output flush so we can see what we're getting *. * / Ob_implicit_flush (); $ Address = '192.168.1.70'; $ Ports = 10000; If (($ sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP)) === Incorrect) {Echo "socket_cent () failed: reason:". Socket_strerror (socket_last_error ()). "\ N"; } If (socket_bind ($ sock, $ address, $ port) === incorrect) {echo "socket_bind () failed: reason:". Socket_strerror (socket_last_error ($ sock)) "\ N"; } If (socket_listen ($ sock, 5) === incorrect) {echo "socket_line () failed: reason:". Socket_strerror (socket_last_error ($ sock)) "\ N"; } Do {if (($ msgsock = socket_accept ($ sock)) === false) {echo "socket_except () failed: reason:". Socket_strerror (socket_last_error ($ sock)) "\ N"; break; } Do {if (false === ($ buf = socket_read ($ msgsock, 2048, php_NORMAL_READ))} {echo "socket_read () failed: reason:". Socket_strerror (socket_last_error ($ msgsock)). "\ N"; Break 2; } If (! $ Buf = trim ($ buf)) {continue; } If ($ buf == 'left') {break; } If ($ buf == 'closed') {socket_close ($ msgsock); Break 2; } $ Talkback = "PHP: You said '$ buf'. \ N"; $ File = fopen ("welcome.txt", "a +") or exit ("Unable to open file!"); Filitt ($ file, $ talkback); Fclose ($ file); Socket_write ($ msgsock, $ talkback, strlen ($ talkback)); Echo "$ buf \ n"; } While (true); Socket_close ($ msgsock); } While (true); Socket_close ($ sock);
You can weaken the script and run it from the command line - Colin As per the comment above - PHP can not be the best language for this - but when I have done it completely, I have added a monitor to see and re-launch the script, if it is running for some reason Be there
If you are running Unix then the script executable eg
chmod a + x & lt; Script name & gt; Add more
#! / Usr / bin / php & lt; The script will allow you to execute it from the command line to start php .
Then use a package like Deamon Tools
To launch and monitor that your script is running.
Comments
Post a Comment