karthik_ppts 81 Posting Pro

Hi Frendz,

This is my code to read data from port using php.

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

    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!";
    }

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

But the output is like this...

Start to Create socket....
Socket Created!
Start to Bind Socket....
Socket Bound!
Warning: socket_read() [function.socket-read]: unable to read from socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in D:\xampp\htdocs\portlistener\portlistener1.php on line 21

What is going wrong with this?

Be a part of the DaniWeb community

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