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 927793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:54:31+00:00 2026-05-15T19:54:31+00:00

Below is partial code to an experimental http server app I’m building from scratch

  • 0

Below is partial code to an experimental http server app I’m building from scratch from a PHP CLI script (Why? Because I have too much time on my hands). The example below more closely matches PHP’s manual page on this function. The problem I’m getting is when connecting to this server app via a browser (Firefox or IE8 from two separate systems tested so far), the browser sends an empty request payload to the server and aborts roughly every 1 in 6 page loads.

The server console displays the “Connected with [client info]” each time. However, about 1 in 6 connections will result in a “Client request is empty” error. No error is given telling the header/body response write to the socket failed. The browser will generally continue to read what I give it, but this isn’t usable as I can’t fulfill the client’s intended request without knowing what it is.

<?php

$s_socket_uri = 'tcp://localhost:80';
// establish the server on the above socket
$s_socket = stream_socket_server($s_socket_uri, $errno, $errstr, 30) OR
    trigger_error("Failed to create socket: $s_socket_uri, Err($errno) $errstr", E_USER_ERROR);
$s_name = stream_socket_get_name($s_socket, false) OR
    trigger_error("Server established, yet has no name.  Fail!", E_USER_ERROR);
if (!$s_socket || !$s_name) {return false;}

/*
   Wait for connections, handle one client request at a time
   Though to not clog up the tubes, maybe a process fork is
   needed to handle each connection?
*/
while($conn = stream_socket_accept($s_socket, 60, $peer)) {
    stream_set_blocking($conn, 0);

    // Get the client's request headers, and all POSTed values if any
    echo "Connected with $peer.  Request info...\n";
    $client_request = stream_get_contents($conn);
    if (!$client_request) {
        trigger_error("Client request is empty!");
        }
    echo $client_request."\n\n";  // just for debugging

    /*
      <Insert request handling and logging code here>
    */

    // Build headers to send to client
    $send_headers = "HTTP/1.0 200 OK\n"
        ."Server: mine\n"
        ."Content-Type: text/html\n"
        ."\n";

    // Build the page for client view
    $send_body = "<h1>hello world</h1>";

    // Make sure the communication is still active
    if ((int) fwrite($conn, $send_headers . $send_body) < 1) {
        trigger_error("Write to socket failed!");
        }

    // Response headers and body sent, time to end this connection
    stream_socket_shutdown($conn, STREAM_SHUT_WR);
    }

?>

Any solution to bring down the number of unintended aborts down to 0, or any method to get more stable communication going? Is this solvable on my server’s end, or just typical browser behavior?

  • 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-15T19:54:31+00:00Added an answer on May 15, 2026 at 7:54 pm

    I tested your code and it seems I got better results reading the socket with fread(). You also forgot the main loop(while(1), while(true) or for(;;).

    Modifications to your code:

    • stream_socket_accept with @stream_socket_accept [sometimes you get warnings because “the connected party did not properly respond“, which is, of course, the timeout of stream_socket_accept()]
    • Added the big while(1) { } loop
    • Changed the reading from the socket from $client_request = stream_get_contents($conn);
      to while( !preg_match('/\r?\n\r?\n/', $client_request) ) { $client_request .= fread($conn, 1024); }

    Check the source code below (I used 8080 port because I already had an Apache listening on 80):

    <?php
    
    $s_socket_uri = 'tcp://localhost:8080';
    $s_socket = stream_socket_server($s_socket_uri, $errno, $errstr, 30) OR
        trigger_error("Failed to create socket: $s_socket_uri, Err($errno) $errstr", E_USER_ERROR);
    $s_name = stream_socket_get_name($s_socket, false) OR
        trigger_error("Server established, yet has no name.  Fail!", E_USER_ERROR);
    if (!$s_socket || !$s_name) {return false;}
    
    while(1)
    {
        while($conn = @stream_socket_accept($s_socket, 60, $peer)) 
        {
            stream_set_blocking($conn, 0);
            echo "Connected with $peer.  Request info...\n";
            //    $client_request = stream_get_contents($conn);
    
            $client_request = "";
            // Read until double \r
            while( !preg_match('/\r?\n\r?\n/', $client_request) )
            {
                $client_request .= fread($conn, 1024);
            }
    
            if (!$client_request) 
            {
                trigger_error("Client request is empty!");
            }
            echo $client_request."\n\n";
            $headers = "HTTP/1.0 200 OK\n"
                ."Server: mine\n"
                ."Content-Type: text/html\n"
                ."\n";
            $body = "<h1>hello world</h1><br><br>".$client_request;
            if ((int) fwrite($conn, $headers . $body) < 1) {
                trigger_error("Write to socket failed!");
                }
        stream_socket_shutdown($conn, STREAM_SHUT_WR);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Partial Code: My below code pulls a query from my DB and then uses
I am using php xpath to get the values from the below xml feed
Below is the code to write to a file after downloading an image from
I cannot get Javascript to run after a ASP.NET postback from a client script
Below is my code: if(settings.controlNav){ $('.nivo-controlNav', slider).hide(); slider.hover(function(){ $('.nivo-controlNav', slider).show('slow'); }, function(){ $('.nivo-controlNav', slider).hide('slow');
Below is my code. For some reason, after the user logs into the little
I am wanting to use jquery to do a show/hide of a DIV from
I have a network server application written in C, the listener is bound using
Below is the use case: I have a unique index defined on 3 columns
Below is an image of the problem. I've tried correcting the folder permissions to

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.