views:

301

answers:

2

I have some simple PHP code that creates a SSL connection

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $this->sslPem);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $this->passPhrase);

$this->apnsConnection = stream_socket_client('ssl://'.$this->apnsHost.':'.$this->apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);

But know how to set SO_KEEPALIVE to true? I've also tried STREAM_CLIENT_PERSISTENT, which is not the same thing.

+1  A: 

Have you verified (via network tracing) that you need to set the socket option?

What are you sending over the socket? HTTP/HTTPS introduces its own connection-reuse feature via the "Connection" header, so the option on the socket isn't necessarily what you want to set.

EricLaw -MSFT-
+1  A: 

SO_KEEPALIVE like in

The SO_KEEPALIVE option causes a packet (called a 'keepalive probe') to be sent to the remote system if a long time (by default, more than 2 hours) passes with no other data being sent or received.
?
I don't know how this is related to STREAM_CLIENT_PERSISTENT but you can set that option with socket_set_option (i.e. using the sockets extension and not streams).

VolkerK
how and which option to set?
erotsppa
That would be socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1). But then again you don't have the ssl support. I haven't found a way to set the option for the ssl-stream. You would need php_stream_set_option for that, but it's an internal function that isn't exposed to the php script.
VolkerK
can't believe PHP does not have a way to do this....
erotsppa
If you find a method to use stream_socket_client() and SO_KEEPALIVE on that stream resource let me know.
VolkerK