Jump to content


Photo

Sockets slowing down apache


  • Please log in to reply
5 replies to this topic

#1 markt

markt
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 August 2006 - 09:27 AM

Hi everyone

I have written a script that connects to an xml service via a socket connection and sends and recieves data encrypted with GPG.
The xml service runs as a daemon on a windows server (arrrgh) powered by .net and invariably crashes - which makes my socket connections run in an infinate loop - which creates more and more apache connections and eventually grinds the server to a half if apache is not restarted.

The connections dont seem to be affected by the php.ini max_execution timout setting, nor anything else that I can find.

Does anyone know how I can timeout a socket connection?

Cheers

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 10 August 2006 - 10:00 AM

set_time_limit(0);

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 markt

markt
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 August 2006 - 11:52 AM

Errrm... no, that sets an unlimited execution time for a script - did you read my post ?

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 10 August 2006 - 01:50 PM

Can we see your code? You should be checking the connection is still true for each loop iteration yeah?

#5 markt

markt
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 11 August 2006 - 08:49 AM

My code is:

function connect_to_socket () {
  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  socket_set_nonblock($this -> SOCKET) or die("Unable to set nonblock on socket\n");
  $time = time();   
   while (!@socket_connect($this -> SOCKET, $this -> IPADDRESS, $this->PORT)) {
     $err = socket_last_error($this -> SOCKET);
     if ($err == 115 || $err == 114){
       if ((time() - $time) >= $this -> TIMEOUT){
         socket_close($this -> SOCKET);
         // connection timed out
         return(false);
       }
       sleep(1);
       continue;
     }
     // connection refused
     return(false);
   }  
   socket_close($socket);
   return (true);
}

This actually works for timing out the socket (improved from yesterday) but I now have issues with reading from the socket when I am using this method (non_block etc). Unfortunaly I dont understand too much about blocking and unblocking sockets and the php manual in non too detailed :/

The code I am using to read/write from the socket (read called after writing data) is:

function read_from_socket() {
    while ($out = socket_read($this -> SOCKET, 12048)) {
      $response .= $out;      
    } 
  return ($response);
}

function write_to_socket($in="") {
  if (!socket_write($this -> SOCKET, $in, strlen($in))) {
    print ("Failed to send data");
  }
}

The response is then acted upon, but the response returned when using nonblock is not the same as if I just do a plain connect (seems to be nothing returned).

Cheers

Mark




#6 markt

markt
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 14 August 2006 - 10:41 AM

Hmmm I guess no one knows then :S




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users