Page 1 of 1

Parallel sockets

Posted: Tue Apr 22, 2008 6:52 pm
by bancek
Hi!

I'm working on PHP MSN bot. When I create new switchboard, i create new proccess. I want to comunicate with these two processes. In switchboard proccess I create socket and authorize and then I use while(TRUE).

Code: Select all

 
while(true){
    if ($msg = $sbsess->rx_im())
...
 
$sbsess->rx_im() ::

Code: Select all

 
function rx_im()
{
  stream_set_timeout($this->sb, 1);
  while (!feof($this->sb)){
   $data = @fgets($this->sb, 4096);
...
 
I want to put this code in in both processes for communication.
http://uk.php.net/manual/en/function.pr ... .php#82130
I don't know how to make two "while-s"!

Thanks for your help, and sorry for my bad english.

Re: Parallel sockets

Posted: Wed Apr 23, 2008 12:01 am
by Mordred
Consider using a pool of non-blocking sockets (with socket_select()) instead. Then you can communicate by simply calling functions.

Re: Parallel sockets

Posted: Wed Apr 23, 2008 2:14 pm
by bancek
I try socket_select, but it doesn't work. I use fsockopen for opening socket. When I select sockets between stdin socket and msn socket in gives me error.

Code: Select all

...
$sb_sock = @fsockopen($server, $port, $errno, $errstr, 5);
...
$stdean = fopen('php://stdin','rb');
$read   = array($sb_sock, $stdean);
var_dump($read);
  while(true)
  { 
    if (socket_select($read, $w = NULL, $e = NULL, 0) > 0) {
...
array(2) {
[0]=>
resource(7) of type (stream)
[1]=>
resource(9) of type (stream)
}

Warning: socket_select(): supplied resource is not a valid Socket resource in /path/to/script/msn_sb.php on line 57
Warning: socket_select(): no resource arrays were passed to select in /path/to/script/msn_sb.php on line 57

Re: Parallel sockets

Posted: Wed Apr 23, 2008 5:12 pm
by bancek
OK, I made it with stream_select, but this is not what I want. This function looks if there's any opend sockets and put them into array. I need something to watch sockets for data and when there is some data, it return which socket contains data.

Re: Parallel sockets

Posted: Wed Apr 23, 2008 5:31 pm
by Mordred
Create the "msn" socket with the socket_* family of functions.

On second thought, stream_select *is* what you need. How are you calling it?
RTFM wrote:The streams listed in the read array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a stream resource is also ready on end-of-file, in which case an fread() will return a zero length string).

Re: Parallel sockets

Posted: Thu Apr 24, 2008 3:12 am
by bancek
Actually, the problem is with stdin. Stream_select can't use stdin for non/blocking. MSN socket works fine.