RALD Posted July 23, 2011 Share Posted July 23, 2011 hello everyone this is my first post and I hope doing this well My problem is this, I am tring to created a PHP application for GPS tracking system, I send the information of the device to my port 5500 in my personal PC, I write a program to read this port and save the data received into a CSV, and works fine, but if the device lost the connection the port does not recognize this disconnection and the program keeps reading this port but does not save into my file, also, I configured the socket to accept 5 connections but only works with one at the time. I am attaching the code if can help me I appreciate it. <?php error_reporting(E_ALL); set_time_limit(60); ob_implicit_flush(); $address = '192.168.0.3'; $port = 5500; $socketcreator = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socketcreator === false) { echo "1 socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; exit; } if (socket_bind($socketcreator, $address, $port) === false) { echo "2 socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; exit; } if (socket_listen($socketcreator, 10) === false) { echo "3 socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; exit; } $archivo = fopen("c:\data\file.csv", "a"); do{ if (($msgsock = socket_accept($socketcreator)) === false) { echo "4 socket_accept() failed: reason: " .socket_strerror(socket_last_error($socketcreator)) . "\n"; exit; } do{ if (false === ($buf = socket_read($msgsock, 10000, PHP_NORMAL_READ))) { echo "5 socket_read() failed: reason: " .socket_strerror(socket_last_error($msgsock)) . "\n"; exit; } if (!$buf = trim($buf)) { continue; } if ($buf == 'quit'){ break 2; }else{ fwrite($archivo, "\n$buf\r\n"); echo $buf."\n"; } }while(true); }while(true) ; fclose($archivo); socket_close($socketcreator); socket_close($msgsock); echo "\n\r the socket is closed \n\r"; ?> Quote Link to comment Share on other sites More sharing options...
phpSensei Posted July 25, 2011 Share Posted July 25, 2011 If you can connect to those ports individually, then create a file for each connection and run a cron job on all 5 files for certain time intervals, and return the results to a database or something, and do what you want from there... I dont know if this is the best way, but I don't have much experience with this sort of stuff using PHP... Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 25, 2011 Share Posted July 25, 2011 There's no reason to hardwire 5 socket connections like this. Why not just recode it to generically allow clients --- it would be much simpler to try and debug the code. You obviously have the rudiments of it working, but I'd suggest you recode to something closer to what they did here: http://www.endyourif.com/how-to-create-a-socket-server-in-php/ and eliminate the weird socket numbering scheme. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted July 25, 2011 Share Posted July 25, 2011 @Gizmoal Seems like it can get pretty heavy on the server... Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 25, 2011 Share Posted July 25, 2011 @Gizmoal Seems like it can get pretty heavy on the server... I don't see a problem for him, it's something he's running on his own workstation as he described. The total number of socket connections can also be limited in the code. Quote Link to comment Share on other sites More sharing options...
RALD Posted July 26, 2011 Author Share Posted July 26, 2011 Hello everyone thank you for respond to my post, I have used the link with the server code and looks great but when I do telnet to the local host and try to type something the connection fail, so I do not know if the code is right..... also, I did forget to mention that I am using a GPRS connection... I do not know what else to do hahaha... I am trapped in this part.... thank you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.