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?