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

19,570

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.

Share:
19,570
Madusanka
Author by

Madusanka

Updated on June 04, 2022

Comments

  • Madusanka
    Madusanka almost 2 years

    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.

  • Dinithi De Silva
    Dinithi De Silva about 11 years
    But I need to do this in an interactive way.I need to get user inputs from both sides with the use of html. If someone knows how to do it please let me know.
  • Ayush
    Ayush about 11 years
    This will work fine via PHP CLI but not in a web environment for the very reason that PHP does not run client-side. If you want it interactive, with HTML you have to use Javascript and Websockets.
  • Admin
    Admin over 10 years
    > PHP is always server-side. Not so. Atypical, but any host with a PHP install can always run php scriptName.php This may not be within a GUI but that too can be resolved via systems like tclTk invoking | scriptName.php and processing the outputs
  • Ayush
    Ayush over 10 years
    @jobeard: How is invoking a script via php scriptName.php not server side? The contrast to server-side will be client-side (as in on the client's browser), and no browser is capable of executing PHP.
  • Barnabas Kecskes
    Barnabas Kecskes over 9 years
    A PHP script is running on the server side. Check. A PHP script can connect to another server. Check. If the PHP from one server can connect to another server, it will be able to act as a client. Check.
  • Srdjan Marjanovic
    Srdjan Marjanovic over 5 years
    PHP most definitely can be a client. Yeah, not the browser kind of client, but can send various types of requests (udp, tcp) to other PHP and non-PHP servers/APIs.