Jump to content

Socket connections


RALD

Recommended Posts

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";
?>

Link to comment
https://forums.phpfreaks.com/topic/242682-socket-connections/
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/242682-socket-connections/#findComment-1246464
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/242682-socket-connections/#findComment-1246486
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/242682-socket-connections/#findComment-1247101
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.