Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4040540
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:45:56+00:00 2026-05-20T12:45:56+00:00

I am using streaming sockets in PHP to read from a remote server. When

  • 0

I am using streaming sockets in PHP to read from a remote server. When the remote server goes away after connection, stream_select continues to show a changed stream on the read portion of the stream, but the data being read in is a blank string.

Here is a small case that reproduces the bug. It is two components, a server and client component.

In order to replicate the bug you will need to do the following using php from the command line:
1. Start up server.php
2. Start up client.php

At this point the server should show ‘Press return to continue…. or CTRL-C’ and the client should show ‘Kill your server. Press return to continue….’

  1. Ctrl-c server.php
  2. Press enter on client.php

At this point you should see debugging output from client.php showing the issue (you will want to ctrl-c pretty quickly, it prints a lot of repeating information very quickly)

I am unsure why the stream_select continues to show the read stream as having changes after the server component is no longer running.

server.php

<?php
  $socket = stream_socket_server("tcp://0.0.0.0:51111", $errno, $errstr);
  $s = stream_socket_accept($socket);

  print("Press return to continue.... or CTRL-C me");
  fread(STDIN,1); // Wait for one character to be pressed.
  fwrite($s, "Yep here's some stuff for you\0");
?>

client.php

<?php
$url = "localhost";
$port = 51111;

$errno = 0;
$errstr = "";

$fp = @stream_socket_client("tcp://".$url.":".$port, $errno, $errstr, 5);
if (!$fp) 
{
  print( "Unable to open socket: $errstr ($errno)\n" );
  throw new Exception( "Unable to open socket : $errstr ($errno)" );
}

//WAIT HERE.
print("Kill your server. ");
print("Press return to continue....\n\n");
fread(STDIN,1); // Wait for one character to be pressed.

stream_set_blocking($fp,0);

$buffer = "";

while ( true )
{
    $read   = array($fp);
    $write  = NULL;
    $except = array($fp);

    //Wait for up to 5 second to get something from the server
    if (false === ($num_changed_streams = stream_select($read, $write, $except, 5)))
    {
        // It timed out!
        fclose($fp);
        print( "Socket internal error\n" );
        throw new Exception( "Socket internal error" );
    }

    if( empty($read) ) //It must be an excecption instead...
    {
        // We got a socket error
        fclose($fp);
        print("Socket error\n");
        throw new Exception( "Socket Error" );
    }

    print( var_export( array( $read, $write, $except), true )."\n" );
    print( "Num changed streams: $num_changed_streams\n" );

    if ( $num_changed_streams == 0 )
    {   
        // nothing changed int he stream, we hit a timeout!
        fclose( $fp );
        print( "Socket timeout\n" );
        throw new Exception( "Socket timeout" );
    }

    //We're ready to read.       
    $chunk = fread($fp, 1024);
    if( $chunk === FALSE )
    {
        print("fread failed\n");
        throw new Exception("fread failed");
    }
    print( "Chnk: ".var_export( $chunk, true )."\n" );

    $buffer.= $chunk;
    if ( (strlen($chunk)>0) && (ord($chunk[strlen($chunk)-1])==0) ) break;
}
fclose($fp);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:45:57+00:00Added an answer on May 20, 2026 at 12:45 pm

    Worked this one out. As per the php documentation at http://au.php.net/manual/en/function.stream-select.php

    The streams listed in the read array
    will be watched to see if characters
    become available for reading (more
    precisely, to see if a read will not
    block – in particular, a stream
    resource is also ready on end-of-file,
    in which case an fread() will return a
    zero length string).

    This 0 length string return case was being triggered when the remote end went away, but was not being caught in the break check.

    Fix is to test the length of the return from fread, and if it is 0 then break out of the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using socket to establish a streaming connection with server. By creating a socket,
I am trying to parse data from commoncrawl.org using hadoop streaming. I set up
I am using Python and Requests to request data from Twitter's streaming API .
When a client disconnects using CTRL-C or the connection is terminated the server should
I'm writing a performance-critical bidirectional streaming server using boost.asio. The server works this way
I'm using what looks to be a real nice API for streaming sockets found
this if my first attempt at using streaming for WCF, and I am struggling
I'm using a streaming distribution on CloudFront to serve media via RTMP. Obviously I
I have a Flex GUI which is using AMF Streaming to BlazeDS on WebLogic
Is there a way to do video streaming using an http url in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.