How to read and write to php socket in one program?

Madusanka picture Madusanka · Feb 25, 2013 · Viewed 19.1k times · Source

Here I wish to pass a message from client to server and write a response back to the client through the server program. Actually I have kept my database at the server side and trying to send data back to the client according to the clients input. The sending part from client to server and retrieving data from the server is ok. I want some idea to write back to the client as a response

Here is my server program

<html>
    <head>

    </head>

    <body>
        <?php
            //socket_close($sock);
            $isread=false;
            $address="127.0.0.1";
            $port="1332";
            $sock=socket_create(AF_INET,SOCK_STREAM,0) or die("Socket creation failed..!");
            socket_bind($sock,$address,$port) or die("Socket binding failed..!");
            socket_listen($sock) or die("Socket listen failed..!");
            $accept=socket_accept($sock) or die("Socket accept failed..!");
            $read=socket_read($accept,1024);
            //echo $read;
            if($read)
                $isread=true;
            $ar=array();
            $ar=explode("@",$read);
            print_r($ar);
            if($isread==true)
                getData('testdata','student',$accept,$ar[0],$ar[1]);

            function getData($dbName,$tblName,$acc,$control,$value)
            {
                mysql_connect('localhost','root','') or die("Connection failed..!");
                mysql_select_db($dbName) or die("Connection failed..!");
                $cmd="SELECT * FROM $tblName WHERE '$control'='$value'";
                $query=mysql_query($cmd) or die("Query execution failed..! ".mysql_error());
                $rows=mysql_num_rows($query);
                $cols=mysql_num_fields($query);
                for($i=0;$i<$rows;$i++)
                {
                    $tbl=mysql_fetch_array($query);
                    for($j=0;$j<$cols;$j++)
                    {
                          socket_write($acc,$tbl[0]+"@");
                    }
                    echo "<br />";
                }
            }
            socket_close($sock);
        ?>
    </body>
</html>

Here is my client program

<html>
    <head></head>
    <body>
        <form action="myclient.php" method="POST">
            <select name="data">
                <option value="RegNo">RegNo</option>
                <option value="Name">Name</option>
            </select>&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="text" name="mytext"></input>&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="Submit" name="btn"></input>
        </form>
    </body>
</html>

<?php
    $iswrite=false;
    if(isset($_POST['btn']))
    {
        $d=$_POST['data'];
        $s=$_POST['mytext'];
        $address="127.0.0.1";
        $port="1332";
        if(isset($d) && isset($s))
        {
            $sock=socket_create(AF_INET,SOCK_STREAM,0) or die("Cannot create socket");
            $con=socket_connect($sock,$address,$port) or die("Cannot connect to socket");

            socket_write($sock,$d);
            socket_write($sock,"@");
            $write=socket_write($sock,$s);
            if($write)
                $iswrite=true;

            if($iswrite)
            {
                $read=socket_read($sock,1024);
                echo $read;
            }

            socket_close($sock);
        }
    }

Please give me an idea.

Answer

Dinithi De Silva picture Dinithi De Silva · Feb 25, 2013

This is how I tried it.

This is the server's program.

<?php
$address="127.0.0.1";
$port="3222";
$sock=socket_create(AF_INET,SOCK_STREAM,0) or die("Cannot create a socket");
socket_bind($sock,$address,$port) or die("Couldnot bind to socket");
socket_listen($sock) or die("Couldnot listen to socket");
$accept=socket_accept($sock) or die("Couldnot accept");
$read=socket_read($accept,1024) or die("Cannot read from socket");
echo $read;
socket_write($accept,"Hello client");
socket_close($sock);
?>

This is the client's program.

<?php
$address="127.0.0.1";
$port="3222";
$msg="Hello server";

$sock=socket_create(AF_INET,SOCK_STREAM,0) or die("Cannot create a socket");
socket_connect($sock,$address,$port) or die("Could not connect to the socket");
socket_write($sock,$msg);

$read=socket_read($sock,1024);
echo $read;
socket_close($sock);
?>

Try this one.This works fine.