views:

37

answers:

2

Okay - I'm beaten.

I have a (PHP-CLI) server written using PHP's socket_* functions. I can connect just fine to it using Putty and it works as expected.

However my PHP-CLI client does not work properly. It seems like the client is trying to grab the socket from the server (yes the server/client are on the same system).

They do seem to connect, but if I have the client set to just receive the server's welcome message it just stalls. If I have the client write after connecting, the server seems to send everything, and the client begins to read properly - but then the server's "socket_read" returns false (meaning an error) but with the error message "The operation completed successfully".

Here is my general algorithm:

SERVER

$this->_clientSock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($this->_clientSock, $this->_address, $this->_port); // Port is 50000 
socket_listen($this->_clientSock, 5);
$this->_clientMsgSock = socket_accept($this->_clientSock);
$msg ="Welcome";
socket_write($this->_clientMsgSock, $msg, strlen($msg));

do
{
  while ($buffer = socket_read($this->_clientMsgSock, $this->_readSize, PHP_NORMAL_READ))
     $inMsg .= $buffer;

  $msg ="You sent '$inMsg '";
  socket_write($this->_clientMsgSock, $msg, strlen($msg));
}while ($msg != "quit");

CLIENT

$this->_serverSock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($this->_serverSock , $this->_address, $this->_port);

socket_write($this->_serverSock , $msg, strlen($msg));  // With this line, the server "socket_read" returns false otherwise the client hangs


while ($buffer = socket_read($this->_serverSock , $this->_readSize, PHP_NORMAL_READ))
         $inMsg .= $buffer;
 print "The welcome message is $inMsg";
A: 

Seems to me you are missing at least socket_connect()

Brimstedt
I think it's been added since. Did adding socket_connect() fix the issue?
bobber205
It was in the code - just not in the question. The code is not a direct copy/paste - more of an algorithm/pseudo code
ChronoFish
A: 

I changed the receive type to "PHP_NORMAL_READ" and made sure that I am sending "\n" on each write.

This seems to have cured the problem that I was witnessing. My client and server are now properly communicating.

ChronoFish