[RESOLVED] fsockopen catch error
Results 1 to 7 of 7

Thread: [RESOLVED] fsockopen catch error

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    11

    resolved [RESOLVED] fsockopen catch error

    Morning. Im trying to check if i can connect via telnet and echo a string depending on the result. The problem is it shows the error message followed by echoes

    PHP Code:
        $usenet fsockopen($cfgServer$cfgPort$errno$errstr$cfgTimeOut);      
    if(!
    $usenet){     echo "failed";     echo "  ".date('H:i:s'time()+3600);}     
    else { echo 
    "connected"; } 
    result

    Warning: fsockopen(): unable to connect to 213.***.***.***:**** (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.) in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\projects\Testing2.0\privatepingtelnet.php on line 17 failed 09:27:27
    expected result

    failed 09:27:27
    How to properly catch the error ?

  2. #2
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Never mind
    PHP Code:
    @fsockopen 

  3. #3
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,715
    It's not generally recommended to do that (@foobar), but as long as you are aware, I've nothing personal against the Q-n-D ;-)

    That being said, you might be well-rewarded (depending on how much of your work is PHP-related) to look at PHP's [url=http://www.php.net/manual/en/book.errorfunc.php]error handling[/php] in general, or the Exceptions section if you have PHP 5 (which you should if at all possible, I think).

    When you said "fsockopen catch error", I did kind of assume you were actually calling try() .... catch() and had an issue, so I was interested in taking a look as I'm not actually using that construct as a habit myself just yet ...
    /!!\ 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

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Tried actually trying to catch it , didn't do anything.

  5. #5
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,943
    Quote Originally Posted by Stobor View Post
    Tried actually trying to catch it , didn't do anything.
    Yeah, you'd have to actually implement a custom error handler that turns it into an exception. As far as I'm concerned, the "@" is not a big deal as long as you are explicitly checking the return value for an error and handling it yourself, as you are in this case. Purists may differ with me.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  6. #6
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    Quote Originally Posted by NogDog View Post
    Yeah, you'd have to actually implement a custom error handler that turns it into an exception.
    I wish PHP did a lot more with exceptions retroactively rather than only moving forward. Either way, I suppose one might load a file via auto_prepend_file that looks like:
    PHP Code:
    <?php
    function exception_error_handler($errno$errstr$errfile$errline ) {
        throw new 
    ErrorException($errstr$errno0$errfile$errline);
    }
    set_error_handler("exception_error_handler");
    to take care of that.

    (Now that I think about that, it actually seems pretty nifty... not sure how backwards-compatible it is, though.)

    Quote Originally Posted by NogDog View Post
    As far as I'm concerned, the "@" is not a big deal as long as you are explicitly checking the return value for an error and handling it yourself, as you are in this case.
    Agreed. Suppressing errors for the sake of gracefully handling them is one thing. Suppressing errors for the sake of hiding errors (e.g. sweeping them under the rug), however, is not.

  7. #7
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,657
    Quote Originally Posted by bradgrafelman View Post
    Agreed. Suppressing errors for the sake of gracefully handling them is one thing. Suppressing errors for the sake of hiding errors (e.g. sweeping them under the rug), however, is not.
    Except that the supressing errors to handle them might inadvertently lead to sweeping others under the rug.

    PHP Code:
    $f = @fsockopen(TypoClass::ip);
    if (!
    $ferror_log('fsockopen failed'); 
    Since @ will now hide
    Fatal error: Class 'TypoClass' not found

    As long as the script is short and stand alone, it will most likely cause no problems. But if (when) you make it part of a larger context, or make it callable from elsewhere you risk having hard to find errors or no notification of them.

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
  •