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

  • Home
  • SEARCH
  • 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 8428783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:09:04+00:00 2026-06-10T05:09:04+00:00

I’m trying to use fsockopen() to check if a Selenium Server is running. When

  • 0

I’m trying to use fsockopen() to check if a Selenium Server is running. When the server is running the constant should be set. When it’s not the constant isn’t set. This is done using the following code:

if(!defined('TEST_SELENIUM')){
    $fp = @fsockopen('localhost', 4444);
    if ($fp !== false) {
        define('TEST_SELENIUM', true);
        fclose($fp);
    }
}

The problem comes up when the server is not running (on Windows 7). Rather than fsockopen() returning false from finding a closed port, it stall in trying to communicate with the port. When the server is running it quickly shows up using netstat. When the server is off netstat does not return anything from port 4444. From my understanding of fsockopen(), this should then immediately return false. But again, it just stalls in trying to communicate with the port. I don’t want to add a timeout here as this is not a point in the application that there should be a stall of any sort. Also, I should note that this appears to work the way I would expect it should on Linux. However, it’s failing here on Windows 7. Can anyone tell me what I’m doing wrong? Thank you much!

  • 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-10T05:09:05+00:00Added an answer on June 10, 2026 at 5:09 am

    You cannot do this without adding a timeout. This is nothing to do with PHP’s implementations – it is exactly how a TCP socket connection is supposed to work.

    Because TCP uses acknowledgements (ACK) to guarantee packet delivery and ordering, when you initiate a TCP connection the client sends a SYN packet, and waits for a returned SYN/ACK packet. The client will continue to wait for this returned packet until either it is received, or the client decides it has waiting long enough and gives up – this is the timeout argument that fsockopen() accepts.

    When you run netstat, it looks at the applications bound to the TCP stack on the local machine – this means that it is able to determine whether a port is in use without attempting to connect to it. As a result, it is able to report instantly whether the socket is in use or not.

    Realistically here you have two options.

    • Supply a timeout. Bear in mind that the timeout argument can be a float (and can therefore be less than 1 second), so if you are checking the local machine, you can set this extremely low. Just playing around testing it on my Windows XP machine, I find that I can set it to 0.001 (1ms) and it successfully connects to services that are running, and returns FALSE for unbound ports. This 1ms timeout is also working for other machines on my (admittedly relatively quiet and fully Gigabit) LAN. YMMV, but 5ms (0.005) should be more than enough in a high speed network.
      For example, this line works nicely for me, and will cause the script to wait for a maximum of 1ms:
    define('TEST_SELENIUM', (bool) @fsockopen('localhost', 4444, $en, $es, 0.001));
    
    • shell_exec('netstat'); and parse the output. This would certainly only work if you were checking the local machine, and would likely not be any faster than the 1ms timeout approach. It is also not very portable, as the netstat utility on various OSes may produce very slightly different output – different column ordering, different separator characters, etc etc.

    It is also worth noting that if the TCP stack is configured to actively reject packets sent to ports that are unbound (by returning an RST packet), then fsockopen() will fail immediately. This is probably why you say this appears to work the way I would expect it should on Linux – the Linux instance you tested it on was configured in this way. There is probably some registry setting that would allow you to configure Windows to behave in this way as well, although off the top of my head I don’t know what it is – the guys over at SuperUser can help you with that.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.