简体   繁体   English

PHP fsockopen() 失败未打开端口但 telnet 工作

[英]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.在过去 2 天里,我正面临一个奇怪的网络问题,试图解决这个问题。 I can not get to open the port 25003 on php web page.我无法在 php 网页上打开端口 25003。 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.端口 80 显示打开但不显示端口 25003。

The site is hosted on Bluehost shared hosting plan with a static IP too.该站点也托管在具有静态 IP 的 Bluehost 共享托管计划上。 Cross verified with them more than 3 times the fact that port 25003 is open on both incoming and outgoing connections.与他们交叉验证了端口 25003 在传入和传出连接上都打开的事实的 3 倍以上。 Would they be lying, I don't think so.他们会不会撒谎,我不这么认为。

On client PC settings :在客户端 PC 设置上:

(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.问题是当我 fsockopen 使用上面的代码片段时,它说连接被拒绝。

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.我认为问题在于 bluehost 共享服务器或客户端 Windows PC 或 SERPROXY 或本地网络配置。 I am afraid there is any major config change possible other than baud rate, com port etc in the SERPROXY which is set correctly.恐怕除了正确设置的 SERPROXY 中的波特率、com 端口等之外,可能还有任何重大的配置更改。

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.如果有人想检查连接,我会共享公共 IP。

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 );
?>

所示两种方法的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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