Click here to Skip to main content
15,072,051 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
I have developed a PHP Socket Server in which I am receiving data from GSM Modules and storing it in MySQL. I want to make it more clever as soon as we get the data we send it to another server at the same time. For the second option I want user to give the host ip address and port to send data. I developed a user input page but I dont know how to configure it. This is my server code. I dont want to restart my server everytime when I change the Ip Address and port of the second host on which i want to send data.


PHP
<?php
    error_reporting(~E_NOTICE);
    set_time_limit(0);
    session_start();

    require ('manageData.php');
    require ('clientInfo.php');
    require ('dataCheck.php');
    require ('endsession.php');
    require ('sendhost.php');

    $address = "0.0.0.0";
    $port = 9000;
    $max_clients = 10;

    if (!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);
        die("Couldn't create socket: [$errorcode] $errormsg \n");
    }
    // Bind the source address
    if (!socket_bind($sock, $address, $port)) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);
        die("Could not bind socket : [$errorcode] $errormsg \n");
    }
    if (!socket_listen($sock, 10)) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);
        die("Could not listen on socket : [$errorcode] $errormsg \n");
    }
    //array of client sockets
    $client_socks = array();
    //array of sockets to read
    $read = array();
    //start loop to listen for incoming connections and process existing connections
    while (true) {
        //prepare array of readable client sockets
        $read = array();
        //first socket is the master socket
        $read[0] = $sock;
        //now add the existing client sockets
        for ($i = 0;$i < $max_clients;$i++) {
            if ($client_socks[$i] != null) {
                $read[$i + 1] = $client_socks[$i];
            }
        }
        //now call select - blocking call
        if (socket_select($read, $write, $except, null) === false) {
            $errorcode = socket_last_error();
            $errormsg = socket_strerror($errorcode);
            die("Could not listen on socket : [$errorcode] $errormsg \n");
        }
        //if ready contains the master socket, then a new connection has come in
        if (in_array($sock, $read)) {
            for ($i = 0;$i < $max_clients;$i++) {
                if ($client_socks[$i] == null) {
                    $client_socks[$i] = socket_accept($sock);
                    if (socket_getpeername($client_socks[$i], $address, $port)) {
                        echo "$address \n";
                        if (strlen($address) > 0) writeToClientInfo("client.csv", $address);
                    }
                    socket_write($client_socks[$i], $message);
                    break;
                }
            }
        }
        //check each client if they send any data
        for ($i = 0;$i < $max_clients;$i++) {
            if (in_array($client_socks[$i], $read)) {
                $input = socket_read($client_socks[$i], 4096);
                if ($input == null) {
                    //zero length string meaning disconnected, remove and close the socket
                    unset($client_socks[$i]);
                    socket_close($client_socks[$i]);
                    writeToEndSession("client.csv", $address);
                }
                $n = trim($input);
                $handle = ',';
                if (strlen($n) > 0) {
                    if (strpos($n, $handle) !== false) {
                        writeToFile('GSM.csv', $n);
                        sendData($n);
                    } else if (strpos($n, $handle) === false || strpos($n, "/") !== false) {
                        writeToRaw('RAW.csv', $n);
                    }
                } else break;
                //send response to client
                socket_write($client_socks[$i], "Data Received \n");
            }
        }
    }
?>


send data function .

PHP
<?php
function sendData($n) {
        date_default_timezone_set("Europe/London");
        $date = date('Y-m-d H:i:s');
        if (isset($_POST['submit'])) {
          $thost = $_GET['hostt'];
          $tport = $_GET['portt'];
        }

        $th = $thost;
        $tp = $tport;

        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($socket === false) {
        echo "socket_create() failed: reason: " .     socket_strerror(socket_last_error()) . "\n";
        }

        $conn = socket_connect($socket, $th, $tp);
        if ($conn === false) {
            echo "socket_connect() failed.\nReason: ($conn) " . socket_strerror(socket_last_error($socket)) . "\n";
        }

        if($th !== 0) echo "Settings Saved Successfully";
        else echo "Error Saving the Settings";

        socket_write($socket, $n, strlen($n));
?>


and html code for input

HTML
<form action="sendhost.php" id="login-form" name="form1" method="post">
<br> Server IP:
<br>
<center>
  <input type="text" value="" class="form-control required" name="hostt" />
</center>
<br> Server Port:
<br>
<center>
  <input type="text" value="" class="form-control required" name="portt" />
<br>
</center>
<br>
<center>
<div class="group submit">
<label class="empty"></label>
<div><input type="submit" value="Save Details" action="sendhost.php"/></div>
</div>
</center>
</form>
Posted
Comments
Muhammad Azym 15-Dec-15 7:37am
   
Do I need to implement socket.io for this purpose to capture the data and then process the data on the client side
Member 14490735 10-Jun-19 2:30am
   
Hi can you share your code if you don't mind.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900