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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:15:10+00:00 2026-06-06T06:15:10+00:00

I’m using the PHP sockets extension (basically a wrapper around the socket(2) -related linux

  • 0

I’m using the PHP sockets extension (basically a wrapper around the socket(2)-related linux syscalls) and would like to re-use sockets I open while serving one request in the subsequent ones. Performance is a critical factor.

The sockets I open are all to the same IP, which makes the use of other functions like pfsockopen() impossible (because it reuses same single socket every time) and I need several at a time.

The question

If I leave the sockets I open serving one request deliberately open, (I don’t call socket_close() or socket_shutdown()) and connect a socket with the exact same parameters to the same IP serving the next request; will linux re-use the previously opened socket / file-descriptor?

What I want to do in the end is to avoid TCP-handshakes on every request.

Additional information:

  • I use the apache worker MPM – which means that different request can be but are not necessarily served from different processes. For the sake of simplicity let’s assume that all requests are served from the same process.

  • I can get the file-descriptor ID of a open and connected socket in PHP. I can open and read and write to /dev/fd/{$id}, yet to no purpose – it’s not communicating with the remote server (maybe this is a naïve approach). If anybody knew how to make this work I’d consider that to be an acceptable answer too.

  • 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-06-06T06:15:11+00:00Added an answer on June 6, 2026 at 6:15 am

    While the answer given by Jirka Hanika is correct for most systems, I have come to the conclusion that it regretfully does not apply to PHP; the re-use of sockets using the PHP sockets extension is impossible to achieve from user space.

    The code that led to this conclusion is:

    function statDescriptors() {
        foreach (glob('/proc/self/fd/*') as $sFile) {
            $r = realpath($sFile);
            // ignore local file descriptors
            if($r === false) {
                echo `stat {$sFile}`, "\n";
            }
        }       
    }
    
    header('Content-Type: text/plain');
    
    statDescriptors();
    
    $oSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    
    socket_set_option($oSocket, SOL_SOCKET, SO_KEEPALIVE, 1);
    socket_set_option($oSocket, SOL_SOCKET, SO_REUSEADDR, 1);
    
    socket_connect($oSocket, '173.194.35.33', 80); // Google IP
    
    socket_write($oSocket, 'GET / HTTP/1.0' . "\r\n");
    socket_write($oSocket, 'Connection: Keep-Alive' . "\r\n\r\n");
    
    socket_read($oSocket, 1024 * 8);
    
    // socket_close($oSocket); // toggle this line comment during test
    
    echo 'PID is: ', getmypid(), "\n";
    
    statDescriptors();
    

    This code will stat() the current process’ open socket file descriptors at the start and end of its’ execution. In between it will open a socket with SO_KEEPALIVE set, write a request to it and read a response. Then it will optionally close the socket (toggle line comment) and echo the current process’ PID (to make sure you’re in the same process).

    You will see that regardless if you close the socket or not, the file descriptor created serving the previous request will not exist anymore at the beginning of this cycle’s execution and a completely new socket will be created and connected.

    I was unable to test SOCK_CLOEXEC since it’s not (yet?) implemented in the extension.

    (This was tested using PHP 5.4.0)

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

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.