php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22656 socket_set_blocking() won't work?
Submitted: 2003-03-12 02:32 UTC Modified: 2003-03-13 06:23 UTC
From: thejoshes at josh dot com Assigned:
Status: Not a bug Package: Sockets related
PHP Version: 4.3.1 OS: OS X 10.2.3
Private report: No CVE-ID: None
 [2003-03-12 02:32 UTC] thejoshes at josh dot com
The following script produces this error:

<b>Warning</b>:  socket_set_blocking(): supplied resource is not a valid stream resource in <b>/Users/josha/test/noblock.php</b> on line <b>10</b><br />

Even though the socket connection will work fine... I expect this to work, since $s is indeed a valid stream resource, right?

#!/usr/local/bin/php
<?php

$HOST = 'localhost';
$PORT = 4005;

# set up the server
$s = socket_create(AF_INET, SOCK_STREAM, 0);
$ret = socket_bind($s, $HOST, $PORT);
$ret = socket_set_blocking($s, false); // why is this an issue?
$ret = socket_listen($s, 1);
echo "Waiting for connection(s) on PORT $PORT...\n";

while (!$done) {
	$u = pollNewConn();
	echo "$u\n";
}

function pollNewConn() {
	global $s;

	if ($conn = socket_accept($s)) {
		return "Connected";
	} else {
		return "No connection";
	}
} 
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-12 03:29 UTC] wez@php.net
I was going to suggest reading the manual, however this point is not made clear:

socket_set_blocking() has nothing to do with the sockets extension, and that is the reason that we changed its name:

http://www.php.net/manual/en/function.socket-set-blocking.php

http://www.php.net/manual/en/function.stream-set-blocking.php

To acheive non-blocking "ext/sockets", you need to call this function:
http://www.php.net/manual/en/function.socket-set-nonblock.php
 [2003-03-12 04:21 UTC] thejoshes at josh dot com
Interesting. When I use socket_set_nonblock($s); instead, I get the following error, repeatedly:

<b>Warning</b>:  socket_accept() unable to accept incoming connection [35]: Resource temporarily unavailable in <b>/Users/josha/test/noblock.php</b> on line <b>27</b>
 [2003-03-13 06:23 UTC] wez@php.net
Not a bug in PHP, as discussed in private mail.
Setting status to bogus.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Apr 09 21:01:23 2020 UTC