|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63293 Add support to establish > 1 persistent connection to the same UNIX socket
Submitted: 2012-10-16 19:12 UTC Modified: 2012-10-17 10:39 UTC
Avg. Score:4.9 ± 0.3
Reproduced:5 of 6 (83.3%)
Same Version:2 (40.0%)
Same OS:5 (100.0%)
From: samm at os2 dot kiev dot ua Assigned:
Status: Open Package: Streams related
PHP Version: master-Git-2012-10-16 (Git) OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
26 + 18 = ?
Subscribe to this entry?

 [2012-10-16 19:12 UTC] samm at os2 dot kiev dot ua
I found that it is possible to establish > 1 persistent connections with unique_id to TCP socket using syntax provided below:

$s1=stream_socket_client('tcp://', $errno, $errstr, 3, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT);

$s2=stream_socket_client('tcp://', $errno, $errstr, 3, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT);

id1 and id2 are an unique identifiers which i can use later. This way i can use > 1 persistent connection to the same host->port. Unfortunately it does not work for the UNIX socket type. My proposed change is to add optional columnn (:) to the UNIX socket and use it as identifier. E.g. 

$s1=stream_socket_client('unix:////var/run/unix.sock:id1', $errno, $errstr, 3, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT);
$s2=stream_socket_client('unix:////var/run/unix.sock:id2', $errno, $errstr, 3, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT);

Part before column is used as socket path name, path after columnt - as connection id.

Test script:
$sock = stream_socket_client('unix:////var/run/unix.sock:234', $errno, $errstr, 3, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT);
if($errno) echo $errstr."\n";
$sock2 = stream_socket_client('unix:////var/run/unix.sock:123', $errno, $errstr, 3, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT);
if($errno) echo $errstr."\n";
// here we can use "netstat -an|grep unix.sock"  to control number of established connections

Expected result:
no connection errors and 2 different connections

Actual result:
connection error


add-column-to-unixsocket (last revision 2012-10-16 19:14 UTC by samm at os2 dot kiev dot ua)

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-17 10:39 UTC]
-Package: Sockets related +Package: Streams related
 [2012-10-22 09:50 UTC] sskaje at gmail dot com
Similar feature/change request, but for persistent id customization support for 

I wrote some code as an APNS client which uses stream_socket_client() and have 
STREAM_CLIENT_PERSISTENT on. But if I need more than one client certificate to 
make my code working as a service for more than one app, I have to create 
different stream context. 

The problem is, 

In ext/standard/streamsfuncs.c (ver 5.4.8)

 109     if (flags & PHP_STREAM_CLIENT_PERSISTENT) {
 110         spprintf(&hashkey, 0, "stream_socket_client__%s", host);
 111     }


 131     stream = php_stream_xport_create(host, host_len, REPORT_ERRORS,
 133             (flags & PHP_STREAM_CLIENT_ASYNC_CONNECT ? 
 134             hashkey, &tv, context, &errstr, &err);

'hashkey' is used as a persistent id for connections, in fact, one same hashkey 
shared by many '$context's.

Additional parameter is requested to customize the persistent id use by 

Sample Code:
class spSimpleAPNS{
	protected function connect($key)
		# Create Context
		$ctx = stream_context_create();
		stream_context_set_option($ctx, 'ssl', 'local_cert', $this-

		# Push
		$fp = stream_socket_client($this->servers[$this->dev_mode ? 
'sandbox' : 'product'][$key], $errno, $error, 100, 
		stream_set_blocking($fp, 0);
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Sep 26 22:05:54 2022 UTC