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?

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.

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

Sorry I misread the question.

Sorry I misread the question.

No problem... Any idea about my thread?

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

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

Member Avatar for TechySafi

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);

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.