简体   繁体   中英

PHP fsockopen() fails does not open port but telnet works

I am facing a weird network problem trying to resolve for the last 2 days. I can not get to open the port 25003 on php web page. The code does not seem to be a problem, however, its as given below.

 $host = 'xx.xx.xx.xx'; // statis public ip address assigned by my isp $ports = array(80, 25003); foreach ($ports as $port) { $connection = fsockopen($host, $port); if (is_resource($connection)) { echo '<h2>' . $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "\\n"; fclose($connection); } else { echo '<h2>' . $host . ':' . $port . ' is not responding.</h2>' . "\\n"; } }

Port 80 shows open but not port 25003.

The site is hosted on Bluehost shared hosting plan with a static IP too. Cross verified with them more than 3 times the fact that port 25003 is open on both incoming and outgoing connections. Would they be lying, I don't think so.

On client PC settings :

(1) Firewall is disabled for testing purpose.

(2) Port forwarding is done correctly in router. I assume so because
 I can easily telnet MY PUBLIC IP with a port 25003 within the same
 LAN and from phone using sim card's internet.

(3) I did a port check from https://ping.eu/port-chk/ and it shows open.

(4) Client PC has a serproxy installed for serial to IP & Port.
(5) When I do a port check from above link, serproxy shows following message which seems to be okay on its part.
      * server thread launched
      * server(1) - thread started
      * server(1) - EOF from sock
      * server(1) - Exiting

(6) Again, when I telnet from external lan, it shows above message in Client PC's Serproxy which means it is doing its work properly. And it shows correct data from serial port to cmd line while telneting. 

The problem is when I fsockopen using above pieces of code, it says CONNECTION REFUSED.

Below is my actual code which should try to connect and read data from serial port but CONNECTION REFUSED.

 $fp = fsockopen("tcp://xx.xx.xx.xx", 25003, $errno, $errstr); if (!$fp) { echo "$errstr ($errno)<br />\\n"; } else { if (!feof($fp)) { $weight = trim(fgets($fp, 64)," "); } } echo $weight; fclose($fp);

I think the problem lies either in bluehost shared server or client windows PC or SERPROXY or local network configuration. I am afraid there is any major config change possible other than baud rate, com port etc in the SERPROXY which is set correctly.

I am now totally clueless as to how to resolve the said problem. If somebody could help will be greatly appreciated.

I would share the public IP if somebody wants to check the connectivity.

Like I mentioned - previously when I attempted to connect all attempts failed but both methods below now work so I'd assume the problem is fixed. I'd maybe suggest restarting the webserver and browser(s)

<?php
    set_time_limit( 0 );
    error_reporting( E_ALL );

    $address = 'xxx.xxx.xxx.xxx';
    $port = 25003;


    /* Method #1 */
    $fp = fsockopen( "tcp://{$address}", 25003, $errno, $errstr );
    if( !$fp ) echo "$errstr ($errno)<br />\n";
    else {
        if( !feof( $fp ) ) {
            $weight = trim(fgets($fp, 64)," ");
        }
    }
    printf('<h1>Method #1</h1>Weight: %s<br /><br />',$weight);
    fclose($fp);




    /* Method #2 */
    function geterror(){
        $obj=new stdClass;
        $obj->code=socket_last_error();
        $obj->error=socket_strerror( $obj->code );
        return $obj;
    }
    function negotiate( $socket ) {
        socket_recv( $socket, $buffer, 1024, 0 );

        for( $chr = 0; $chr < strlen( $buffer ); $chr++ ) {
            if( $buffer[ $chr ] == chr( 255 ) ) {
                $send = ( isset( $send ) ? $send . $buffer[$chr] : $buffer[$chr] );

                $chr++;
                if( in_array($buffer[$chr], array(chr(251), chr(252))) ) $send .= chr(254);
                if( in_array($buffer[$chr], array(chr(253), chr(254))) ) $send .= chr(252);

                $chr++;
                $send .= $buffer[$chr];
            } else {
                break;
            }
        }
        if( isset($send)) socket_send($socket, $send, strlen($send), 0);
        if( $chr - 1 < strlen($buffer)) return substr($buffer, $chr);
    }



    $socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );


    try{

        if( $socket && is_resource( $socket ) ){
            if( !socket_connect( $socket, $address, $port ) ){
                $err=geterror();
                throw new Exception( sprintf( 'socket_connect: %s', $err->error ), $err->code );
            } else {

                while( true ){

                    $e=null;
                    $w=null;
                    $r = array( $socket );
                    $c = socket_select( $r, $w, $e, 5 );

                    foreach( $r as $read_socket ) {
                        if( $r = negotiate( $read_socket ) ) {
                            exit( sprintf( '<h1>Method #2</h1>Reading: %s', print_r( $r, true ) ) );
                        }
                    }
                }
            }

        } else {
            $err=geterror();
            throw new Exception( sprintf( 'Failed to create socket: %s',$err->error ), $err->code );
        }

    }catch( Exception $e ){
        printf( "
        <pre>
            <h1>Error: %d</h1>\nMessage: %s\nTrace: %s\nLine: %d
        </pre>",$e->getCode(),$e->getMessage(),$e->getTraceAsString(),$e->getLine() );
    }
    socket_close( $socket );
?>

所示两种方法的输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM