i trying make php script, following things:
- starts listening on designated port connections , messages
- connects designated port, communication
this how looks (i copy parts of script, because spans multiple classes, , hard copy each part):
constructor main object in script
//connecting designated port communication $this->nodeserver = socket_create(af_inet, sock_stream, sol_tcp); socket_set_option($this->nodeserver, sol_socket, so_reuseaddr, 1); if (socket_connect($this->nodeserver,"xxx.xxx.xxx.xxx",'14000') === false) { throw new unexpectedvalueexception("failed connect: " . socket_strerror(socket_late_error())); exit; } //starting listen on designated port $this->socketlistener = socket_create(af_inet, sock_stream, sol_tcp); socket_set_option($this->socketlistener, sol_socket, so_reuseaddr, 1); socket_bind($this->socketlistener, 0, $this->listeningport); socket_listen($this->socketlistener); //adding these 2 sockets(the connected one, , listener one) array $this->addsocket($this->nodeserver); $this->addsocket($this->socketlistener);
in main file, following:
main file
$connectedtonode=true; while ($connectedtonode) { //manage multipal connections $changedread = $phpworker->socketlist; $changedwrite = $phpworker->socketlist; socket_select($changedread, $changedwrite, $null, 0, 10); echo 'this test echo'; // echo nr. 1 //check new socket if (in_array($phpworker->socketlistener, $changedread)) { $socket_new = socket_accept($phpworker->socketlistener); //accpet new socket $phpworker->addsocket($socket_new); //add socket client array $header = socket_read($socket_new, 1024); //read data sent socket socket_getpeername($socket_new, $ip); //get ip address of connected socket echo "someone connected"; // echo nr. 2 echo $ip; // echo nr. 3 //make room new socket $found_socket = array_search($phpworker->socketlistener, $changedread); unset($changedread[$found_socket]); } foreach ($changedread $changed_socket) { //if incomming message, process while(socket_recv($changed_socket, $buf, 1024, 0) >= 1) { echo "this second test"; // echo nr. 4 $socketid = array_search($changed_socket, $phpworker->socketlist); $phpworker->processmessage($socketid, $buf); } $buf = @socket_read($changed_socket, 1024, php_normal_read); if ($buf === false) { // check disconnected client // remove client array $socketid = array_search($changed_socket, $phpworker->socketlist); if (($socketid==0) || ($socketid==1)) { //the first element "nodeserver", second element listener, if either 1 goes away, stop script $connectedtonode=false; } //unset disconnected client unset($phpworker->socketlist[$socketid]); unset($phpworker->socketdata[$socketid]); } } }
i managed connect "nodeserver", , able communicate it, , works fine, can't manage connect php script, listener of php script.
this how tried (another part object):
$this->phphelper = socket_create(af_inet, sock_stream, sol_tcp); socket_set_option($this->phphelper, sol_socket, so_reuseaddr, 1); $hostip=gethostbyname('myhost.name.here'); if (!socket_connect($this->phphelper, $hostip, $portonwhichmainsocketislistening)) { echo socket_strerror(socket_last_error()); socket_clear_error(); $this->phphelper=null; } else { socket_send($this->phphelper, "test\0", strlen("test\0"), 0); }
the echoes in while cycle, there test, , strangely, echo nr 1 fired once, nr.2 , nr.3 never fired, , nr.4 fired each time when send message nodeserver listener php script.
what problem? doing wrong? shouldn't first echo fire @ every loop?
part of script written inspiration page: http://www.phpbuilder.com/articles/application-architecture/optimization/creating-real-time-applications-with-php-and-websockets.html
edit: okay, seems found solution, still don't understand why works:
in while part of below code:
foreach ($changedread $changed_socket) { //if incomming message, process while(socket_recv($changed_socket, $buf, 1024, 0) >= 1) { echo "this second test"; // echo nr. 4 ... } }
i've inserted break 2;
command, gives permission main while cycle, continue executing. way, can connect listener socket other php file, , echo's fire. question then, why execution stuck in inner while , for cycles without break
command?
Comments
Post a Comment