0

i have connected an external device to my server. That device send some data through one port. Now how to read that data from port and store in mysql using php?

3
Contributors
8
Replies
11
Views
6 Years
Discussion Span
Last Post by karthik_ppts
0

PHP is not actually meant for reading from devices, other than host configurations and responses from web users.

If you would like to read from a port, you would probably need to use another programming language. Other programming langugages usually have the capability to communicate with mysql.

0

Oh.. Ok.... then what is the use of this code?

<?php
    $host = "192.168.1.5";
    $port = 4321;
    set_time_limit(0);

    echo "Start to Create socket....<br>";
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("create():".socket_strerror(socket_last_error($socket)));
    if($socket)
    {
        echo "Socket Created!<br>";
    }

    echo "Start to Bind Socket....<br>";
    $result = socket_bind($socket, $host, $port) or die("Bind():".socket_strerror(socket_last_error($socket)));
    if($result)
    {
        echo "Socket Bound!<br>";
    }

    $listen=socket_listen($socket, 5);
    if($listen)
    {
        echo "socket is listened";
    }

    echo "Start to connect socket....<br>";
    $conn=socket_connect($socket,"192.168.1.5",4321);
    if($conn)
    {
        echo "Socket connected!<br>";
    }

    while (TRUE)
    {
        $input = socket_read($socket,1024);
        $input = trim($input);
        echo $input;

        ob_flush(); // use both ob_flush and flush to ensure
        flush(); // output is written to the browser window
    }
    socket_close($socket);
?>

This code returns socket created,bound,listen. But the socket is not connected.

It shows this error.
"Warning: socket_connect() [function.socket-connect]: unable to connect [0]: An invalid argument was supplied."

Edited by Nick Evan: Fixed formatting

0

sorry - haven't worked with sockets before. Thought that you meant through something like a hardware port. Normally I would not have thought that. Hope you find the answer

0

sorry - haven't worked with sockets before. Thought that you meant through something like a hardware port. Normally I would not have thought that. Hope you find the answer

Ok.. No Problem...

0

Instead of this

$conn=socket_connect($socket,"192.168.1.5",4321);
if($conn)
{
echo "Socket connected!<br>";
}

try this

$spawn = socket_accept($socket);
if($spawn)
{
echo "Connection Done!";
}
$input = socket_read($socket,1024);
//now lets store it on mysql 

$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database_name", $con);

mysql_query("INSERT INTO table_name (colum1, colum2)
VALUES ('value_for_colum1', '$input')"); //we stored the input on colum2

mysql_close($con);
0

Yes I tried with this.. Socket accepted.. but its not reading.. connection error

Instead of this

$conn=socket_connect($socket,"192.168.1.5",4321);
if($conn)
{
echo "Socket connected!<br>";
}

try this

$spawn = socket_accept($socket);
if($spawn)
{
echo "Connection Done!";
}
$input = socket_read($socket,1024);
//now lets store it on mysql 

$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database_name", $con);

mysql_query("INSERT INTO table_name (colum1, colum2)
VALUES ('value_for_colum1', '$input')"); //we stored the input on colum2

mysql_close($con);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.