Setting a Connection Timeout on a Socket with socket_create( )
Results 1 to 7 of 7

Thread: Setting a Connection Timeout on a Socket with socket_create( )

  1. #1
    Senior Member l008com's Avatar
    Join Date
    Dec 2003
    Location
    Stoneham MA
    Posts
    110

    Question Setting a Connection Timeout on a Socket with socket_create( )

    First, some background info. What I'm trying to do is essentially a port sniff. I'm trying to connect to a host and determine if a port is open, closed or blocked. I used to do this using the fsockopen( ) function. It lets you set a timeout for initiating the connection, and life was good. But then I started running this website on it's own unique IP address, and so I have to bind the connection to this IP. You can't do this with fsockopen( ) but you can do it with socket_create( ) and related functions.

    So I've switched to using these functions, but now I have a new problem. I seem to be able to create read and write timeouts, but no timeout for actually creating the connection. I've tried setting send and receive timeouts, and setting the default_socket_timeout value. I set both to timeout in 2 seconds, but it still takes 75 seconds to timeout.

    Also to be clear, this code works fine for most hosts. The problem is running it on a host that has the port blocked via firewall. socket_connect( ) eventually returns an error #60, as it should, for a timed-out connection. The problem is how long that error takes (75 seconds).

    PHP Code:
    ini_set("default_socket_timeout","2");
    $sock socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
    socket_bind($sock,OPERATING_IP);
    socket_set_option($sockSOL_SOCKETSO_RCVTIMEO, array("sec" => 1"usec" => 0));
    socket_set_option($sockSOL_SOCKETSO_SNDTIMEO, array("sec" => 1"usec" => 0));
    $sock_status = @socket_connect($sock,$ip,$port); 
    In the above code: OPERATING_IP is a constant of the IP address I am binding to, $ip is the IP address I am connecting to. This will be any user anywhere on the internet. I do not control or know anything about it, $port is the specific port I am testing

    So just to reiterate, how can I set a timeout for actually initiating the TCP connection using socket_connect( )? I tried (in lines 1, 4 & 5) to set it to a short timeout of 2 seconds, but it still takes 75 seconds to timeout, when connecting to hosts that block connections.

  2. #2
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,729
    Nvm, sorry.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  3. #3
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,675
    I don't know anything about connection timeout as relating to the socket_ functions, but I do know you can specify connection timout using curl. Would that perhaps be viable for testing connectability?

    curl_setopt
    CURLOPT_CONNECTTIMEOUT The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.

  4. #4
    Senior Member l008com's Avatar
    Join Date
    Dec 2003
    Location
    Stoneham MA
    Posts
    110
    CURL doesn't really have anything to do with this. CURL is for making HTTP requests. I'm trying to open TCP connections to arbitrary ports that could be running any service, or no service at all.

  5. #5
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,910
    I haven't done any of this, and I'd expect timeout settings to set timeouts, but I think if you set the socket to be non-blocking before trying to connect, execution ought to go straight through the socket_connect call without pausing.

    While the connection attempt is in progress, socket_connect would return false and set socket_last_error to say the connection is in progress.

    I don't know whether this will affect the 75-second timeout, but at least you won't be completely held up (if you're testing multiple ports you could test them concurrently, for example).

    EDIT: I see there's a user note on the socket_connect page describing non-blocking sockets and setting a timeout.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  6. #6
    Senior Member l008com's Avatar
    Join Date
    Dec 2003
    Location
    Stoneham MA
    Posts
    110
    Quote Originally Posted by Weedpacket View Post
    I haven't done any of this, and I'd expect timeout settings to set timeouts, but I think if you set the socket to be non-blocking before trying to connect, execution ought to go straight through the socket_connect call without pausing.

    While the connection attempt is in progress, socket_connect would return false and set socket_last_error to say the connection is in progress.

    I don't know whether this will affect the 75-second timeout, but at least you won't be completely held up (if you're testing multiple ports you could test them concurrently, for example).

    EDIT: I see there's a user note on the socket_connect page describing non-blocking sockets and setting a timeout.
    If I did non-blocking sockets, how would I check the status of the socket? I wasn't able to find any sample code and I have no idea how you'd do this.

  7. #7
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,910
    There's some sample code in the user note I alluded to. Short answer is that you check socket_last_error.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •