0

I'm doing a inter-process communication between PHP and a Python daemon. The python daemon acts as a server. From PHP, I'm sending the initial data, and then the daemon keeps on sending some data until "Stop" is sent and the PHP stops reading and closes the connection.

Python Server:

from socket import socket as sock

s = sock()
host = "localhost"
port = 5903
s.bind((host, port))

print host, port

s.listen(5)
while True:
    c, addr = s.accept()
    print 'Got connection from', addr
    y = c.recv(1024)
    print y
    while True:
        x = raw_input()
        c.send(x)
        if x == "Stop":
            break

PHP Client:

<?php
//ob_implicit_flush();
$host = "localhost";
$port    = 5903;
$message = "Hello";
echo "Message To server :".$message;
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Create Error\n");
$result = socket_connect($socket, $host, $port) or die("Connect Error\n");
socket_write($socket, $message, strlen($message)) or die("Send Error\n");
echo "Reply From Server  :";
while(1){
    $result = socket_read ($socket, 1024) or die("Read Error\n");
    if($result == "Stop")
        break;
    echo "<br />".$result;
}
socket_close($socket);
?>

Whenever I type something at the server and press Enter, There's no output at PHP. Upon terminating the connection at server by pressing ctrl+c, all the sent data is being shown in PHP. I guess it's not showing because it waits for a stream of data of specified length. What I expect PHP client is to show the sent data as soon as received.

I've tried the same thing with a Python client with the below code and it's showing the received data, as soon as it received.

Python Client:

import socket

s = socket.socket()
host = "localhost"
port = 5903

print host, port
s.connect((host, port))
y = raw_input()
s.send(y)
while True:
    x = s.recv(1024)
    if x=="Stop":
        break
    print x
s.close

Is there anyway the same thing can be done with PHP also?

RatDon
  • 3,403
  • 8
  • 43
  • 85
  • Is the PHP script running from the CLI or from a webserver? – Barmar May 11 '15 at 10:31
  • Why are the clients conencting to different ports than the server listens on? – Barmar May 11 '15 at 10:31
  • @Barmar PHP running from the WebServer. Sorry about the port part. With python, after closing the socket, I could reuse the port. But when doing the IPC, it was saying the port is already in use. So the confusion between different versions of the code. Corrected that one. – RatDon May 11 '15 at 10:35
  • 1
    Use the `SO_REUSEADDR` socket option to be able to reuse the same port. – Barmar May 11 '15 at 10:42
  • When you run a PHP script through a webserver, nothing is visible until the script finishes. – Barmar May 11 '15 at 10:43
  • @Barmar Yeah. I get that. I tried with CLI and it's showing as soon as it receive. Is there any work around with this one? With webserver also is it possible to process as soon as the string is received? – RatDon May 11 '15 at 10:52
  • It IS processing it as soon as it's received. But the webserver won't send the output to the browser until the script finishes. – Barmar May 11 '15 at 10:53
  • 1
    Actually, it looks like there's a way: http://stackoverflow.com/questions/4399549/display-output-in-parts-in-php – Barmar May 11 '15 at 10:54
  • @Barmar. Thanks. It's working quite okay. But it's displaying for every two lines. Is it the normal behaviour? – RatDon May 11 '15 at 11:09
  • I have no idea, I've never done it myself. – Barmar May 11 '15 at 11:10

0 Answers0