Re: [PHP] Socket trouble
To prevent the blocking and your CPU going up to 100% usages look into: http://us2.php.net/manual/en/function.socket-select.php For length of a string: http://us2.php.net/manual/en/function.strlen.php - Original Message - From: René Fournier <[EMAIL PROTECTED]> Date: Thursday, October 28, 2004 1:04 pm Subject: [PHP] Socket trouble > I'm having some trouble with my socket client. I would like it to > "notice" when the server is no longer there, and then reconnect. I > have > code for the reconnect, etc., but I can't figure out how to write > the > socket_read code so that it "notices" when the server is gone and > then > responds. Presently, my client just hangs (CPU goes to 100%, > nothing > happens.) > > Here's my code: > > while(($buf = socket_read($socket,1,PHP_BINARY_READ)) !== false) { > $data .= $buf; > if(preg_match("/TERMINATING/",$data)) { > $msg_recv = 1; > break; > } > } > > The PHP docs on socket_read suggest how this might be done: > > "Note: socket_read() may return a zero length string ("") > indicating > the end of communication (i.e. the remote end point has closed the > connection)." > > Does this "end of communication" include cases when the socket > server > dies without gracefully disconnecting? How could my code check for > this > zero length string? Any help is much appreciated. Thanks! > > ...René > > --- > René Fournier > www.renefournier.com > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Socket trouble
I'm having some trouble with my socket client. I would like it to "notice" when the server is no longer there, and then reconnect. I have code for the reconnect, etc., but I can't figure out how to write the socket_read code so that it "notices" when the server is gone and then responds. Presently, my client just hangs (CPU goes to 100%, nothing happens.) Here's my code: while(($buf = socket_read($socket,1,PHP_BINARY_READ)) !== false) { $data .= $buf; if(preg_match("/TERMINATING/",$data)) { $msg_recv = 1; break; } } The PHP docs on socket_read suggest how this might be done: "Note: socket_read() may return a zero length string ("") indicating the end of communication (i.e. the remote end point has closed the connection)." Does this "end of communication" include cases when the socket server dies without gracefully disconnecting? How could my code check for this zero length string? Any help is much appreciated. Thanks! ...René --- René Fournier www.renefournier.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Socket trouble
I have written a browser class, and while the connect method seems to work ok (it times out after 10 seconds, as desired), I'm having problems with actually writing to the socket. For unreliable servers, it has taken up to 15 minutes to write to the socket, whereas I would like the script to give up after only a few seconds. I have tried: if ($this->connection->socket) socket_set_timeout($this->connection->socket, 1); setting the socket timeout to 1 second, but it only serves to aggravate the problem. Am I doing something wrong? Is there some way to get it to give up writing to the socket after a few seconds? [I'm using the latest stable PHP, freshly compiled on Open BSD; socket_set_timeout does appear to be working, but not as I desire. (my browser constructor looks like this: $this->ua=$ua; $this->host=$host; $this->proxy=$proxy; if ($proxy) $this->connection=new connection($proxy,$proxyport,$timeout); else $this->connection=new connection($host); if (!$this->connection->socket) $this->failure=1; // failure :-( // if ($this->connection->socket) socket_set_timeout($this->connection- >socket,1); also class connection { // This is a low-level socket class. function connection($host,$port=80,$timeout=10) { if (!$host) "You need a host!"; $this->socket=fsockopen($host,$port,&$errno,&$errtext,$timeout); } function close() { fclose($this->socket); } function put($string) { if (!$string) die("missing argument \$string to connection->put"); if (!$this->socket) return 0; fputs($this->socket,$string); return 1; } function get($packet=128) { // Packet size if (!$this->socket) return 0; return fgets($this->socket,$packet); } } ) Free, encrypted, secure Web-based email at www.hushmail.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]