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

Related Questions

I am using an editortemplate, I have shown partial code below. The script tag
newbie here. I successfully created a chat server using c# (server partial code below,
Partial Code: My below code pulls a query from my DB and then uses
Below is the code from internalRegister method of GCMRegistrar class static void internalRegister(Context context,
below is the code to download a txt file from internet approx 9000 lines
Below is the code from a plugin I use for sitemaps. I would like
I have an MVC app with a Partial View (below). When I add Html.EnableClientValidation(),
I know that the below code is a partial specialization of a class: template
Below you can see my .xaml.cs code. The app opens fine. There are 4
I am using php xpath to get the values from the below xml feed

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.