php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25309 stream_set_blocking() doesn't set non-blocking mode
Submitted: 2003-08-29 06:38 UTC Modified: 2003-08-29 07:18 UTC
From: flugelaar at pandora dot be Assigned:
Status: Not a bug Package: Sockets related
PHP Version: 5CVS-2003-08-29 (dev) OS: Linux Slackware 9.0
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 !
Your email address:
MUST BE VALID
Solve the problem:
33 + 34 = ?
Subscribe to this entry?

 
 [2003-08-29 06:38 UTC] flugelaar at pandora dot be
Description:
------------
Fgets() keeps blocking when I try to make a socket from stream_socket_accept() non-blocking. The listening socket is also non-blocking.

Reproduce code:
---------------
This code is still blocking on the fgets:

    $conn = @stream_socket_accept($hsock,1,&$host);
    if (!$conn) return;
    echo "Got connection from ".$host."\n";
    stream_set_blocking($conn,false);
    $get = '';
    $loop = 0;
    while ($loop <= 10) { $getl = trim(fgets($conn)); $loop++; if (empty($getl)) { echo "End of headers\n"; break; }else{ $get .= $getl."\n"; } }

This is the code that created the $hsock :

$hsock = stream_socket_server('tcp://'.$bind.':'.$port, $errno, $errstr);
if (!$hsock) die ('Listening socket failed: '.$errstr);
if (!socket_set_blocking($hsock,false)) echo "Socket (listening) set nonblock failed\n";

$bind is 0.0.0.0 and port is 3333

Expected result:
----------------
It shouldn't hang on the fgets() because this script handles two sockets. And the other one times out when fgets() hangs untill something is recieved.

Actual result:
--------------
Fgets() keeps blocking so my other connection times out.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-29 07:18 UTC] wez@php.net
fgets() works in a greedy-read mode, so it will keep reading until it gets the complete line (up to the size you requested).

If you are using multiple sockets, you should be using blocking sockets and stream_select(), and not using fgets() but instead fread().


 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu May 28 09:01:24 2020 UTC