|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28136 Problems with connection of a non blocking socket
Submitted: 2004-04-24 16:56 UTC Modified: 2005-02-03 01:00 UTC
Avg. Score:4.4 ± 0.9
Reproduced:7 of 7 (100.0%)
Same Version:4 (57.1%)
Same OS:6 (85.7%)
From: daniele-dll at yahoo dot it Assigned:
Status: No Feedback Package: Sockets related
PHP Version: 5.0.0RC1 OS: Windows\Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2004-04-24 16:56 UTC] daniele-dll at yahoo dot it
Hi to all

i'm tring to write a little application with PHP using Socket, the external module, not fsockopen function or similar.

I've noticed that, with this, and previous version of php, that after i create a socket and set it as non blocking socket, when i try to connect using

socket_get_option($SOCKET, SOL_SOCKET, SO_ERROR)

i recive EVER 0
Instead i should recive 115
If socket_get_option, with these params, return 0, it mean that connection is done and i can send datas, but if i send datas i get a warning that say that socket is in a connection state so i can't send datas.

There is some parts of code:
$this->socket[$sock_id]['resource'] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!is_resource($this->socket[$sock_id]['resource']))
				die("Unable to create socket! " . socket_strerror(socket_last_error()) . "\r\n");
if (!socket_set_option($this->socket[$sock_id]['resource'], SOL_SOCKET, SO_REUSEADDR, 1)) {
				die("Unable to set to as REUSABLE socket! " . socket_strerror(socket_last_error($this->socket[$sock_id]['resource'])) . "\r\n");
after i set socket as reusable (but php gimme me the same problem without this line too) and, after, if i need, i bind socket to a specific IP and PORT

after there is this code:
			@socket_connect($this->socket[$sock_id]['resource'], $this->socket[$sock_id]['remote_host'], $this->socket[$sock_id]['remote_port']);
while (($ris = @socket_get_option($this->socket[$sock_id]['resource'], SOL_SOCKET, SO_ERROR)) == 115) {
	echo "--> " . $ris . "\r\n";
echo "\r\n--> " . $ris . "\r\n";

If you run this code it will exit immedatly printing:

--> 0

Infact, after, if you try to write to socket, you will get a warning

I haven't modified php.ini, and i've tried to run this code, using only socket extension, on windows and on linux
I use apache 1.3.29 on windows and on linux



Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-10 00:43 UTC] snodge at custard dot org
Hi - I have the same issue, althought I am using PHP 5.0RC3 (cli) from Linux. Here is some of the code that I am using - I added an echo statement so that I could see what was happening when I connected with more than one client at a time:

$port = 5000;

if (!$sock = socket_create_listen($port)) {
   echo socket_strerror(socket_last_error());

if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
   echo socket_strerror(socket_last_error());

if (!socket_set_nonblock($sock)) {
   echo socket_strerror(socket_last_error());

while	(true)
	$msgsock == FALSE;
		@$msgsock = socket_accept($sock);
	} while ($msgsock == FALSE);
   echo "Received connection from $msgsock\n";
 [2005-01-26 04:49 UTC]
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc.

If possible, make the script source available online and provide
an URL to it here. Try to avoid embedding huge scripts into the report.

Also try the latest CVS snapshot from

 [2005-02-03 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2007-01-20 00:13 UTC] fakeman at i dot ua
You wrote incorrect code a little. You must not check return value of socket_get_option after socket_connect immediately because some operations (like socket creation) take a long time to execute, mostly because they involve talking to another host, which may have an exruciating long timeout on it.
You should use socket_select and when select (or related interface) says that the socket is ready for writing, you can call socket_get_option($SOCKET, SOL_SOCKET, SO_ERROR, ...) on it. And now socket_get_option will return with the error socket_connect would have indicated, had it been called in a blocking manner. If the error is 0, the connect succeeded.
As you remember socket_select have timeout paramer and this parameter give some time to connect or no socket.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jul 24 08:01:31 2024 UTC