如何对socket进行时间限制?

发布于 2024-12-25 12:06:52 字数 1002 浏览 0 评论 0原文

如何对socket进行时间限制? http://pastebin.com/0q3NeLAX

我尝试了 socket_time_limit 和其他方法,但没有帮助。

我希望如果套接字没有收到任何信息,它将在 X 秒后关闭。

function QueryMinecraft( $IP, $Port = 25565 )
{
    $Socket = Socket_Create( AF_INET, SOCK_STREAM, SOL_TCP );
    if( $Socket === FALSE || @Socket_Connect( $Socket, $IP, (int)$Port ) === FALSE )
    {
        return FALSE;
    }

    Socket_Send( $Socket, "\xFE", 1, 0 );
    $Len = Socket_Recv( $Socket, $Data, 256, 0 );
    Socket_Close( $Socket );

    if( $Len < 4 || $Data[ 0 ] != "\xFF" )
    {
        return FALSE;
    }

    $Data = SubStr( $Data, 3 );
    $Data = iconv( 'UTF-16BE', 'UTF-8', $Data );
    $Data = Explode( "\xA7", $Data );

    return Array(
        'HostName'   => SubStr( $Data[ 0 ], 0, -1 ),
        'Players'    => isset( $Data[ 1 ] ) ? IntVal( $Data[ 1 ] ) : 0,
        'MaxPlayers' => isset( $Data[ 2 ] ) ? IntVal( $Data[ 2 ] ) : 0
    );
}

How to make socket time limit ?
http://pastebin.com/0q3NeLAX

I tried socket_time_limit and others, but didnt help.

I want that if socket does not received any information, it will be closed after X seconds.

function QueryMinecraft( $IP, $Port = 25565 )
{
    $Socket = Socket_Create( AF_INET, SOCK_STREAM, SOL_TCP );
    if( $Socket === FALSE || @Socket_Connect( $Socket, $IP, (int)$Port ) === FALSE )
    {
        return FALSE;
    }

    Socket_Send( $Socket, "\xFE", 1, 0 );
    $Len = Socket_Recv( $Socket, $Data, 256, 0 );
    Socket_Close( $Socket );

    if( $Len < 4 || $Data[ 0 ] != "\xFF" )
    {
        return FALSE;
    }

    $Data = SubStr( $Data, 3 );
    $Data = iconv( 'UTF-16BE', 'UTF-8', $Data );
    $Data = Explode( "\xA7", $Data );

    return Array(
        'HostName'   => SubStr( $Data[ 0 ], 0, -1 ),
        'Players'    => isset( $Data[ 1 ] ) ? IntVal( $Data[ 1 ] ) : 0,
        'MaxPlayers' => isset( $Data[ 2 ] ) ? IntVal( $Data[ 2 ] ) : 0
    );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌生 2025-01-01 12:06:52

我不会为此使用套接字扩展,我仅将其用于无法使用 fsockopen() 完成的高级套接字操作。其原因是套接字扩展并不总是可用,而 fsockopen() 通常可用。

以下是我编写代码的方式,其中包含数据接收超时:

function QueryMinecraft( $IP, $Port = 25565 )
{

    // Seconds to wait for a successful connection
    $connectTimeout = 5;
    // Seconds to wait for data
    $receiveTimeout = 5;

    $Socket = fsockopen($IP, $port, $errNo, $errStr, $connectTimeout);

    if( !$Socket || !stream_set_timeout( $Socket, $receiveTimeout ) )
    {
        return FALSE;
    }

    fwrite( $Socket, "\xFE" );
    $Data = fread( $Socket, 256 );
    fclose( $Socket );

    if( strlen( $Data ) < 4 || $Data[ 0 ] != "\xFF" )
    {
        return FALSE;
    }

    $Data = SubStr( $Data, 3 );
    $Data = iconv( 'UTF-16BE', 'UTF-8', $Data );
    $Data = Explode( "\xA7", $Data );

    return Array(
        'HostName'   => SubStr( $Data[ 0 ], 0, -1 ),
        'Players'    => isset( $Data[ 1 ] ) ? IntVal( $Data[ 1 ] ) : 0,
        'MaxPlayers' => isset( $Data[ 2 ] ) ? IntVal( $Data[ 2 ] ) : 0
    );

}

I would not use the sockets extension for this, I only use that for advanced socket operations that cannot be done using fsockopen(). The reason for this is that the sockets extension is not always available, whereas fsockopen() usually is.

Here is how I would write your code, with a data receive timeout:

function QueryMinecraft( $IP, $Port = 25565 )
{

    // Seconds to wait for a successful connection
    $connectTimeout = 5;
    // Seconds to wait for data
    $receiveTimeout = 5;

    $Socket = fsockopen($IP, $port, $errNo, $errStr, $connectTimeout);

    if( !$Socket || !stream_set_timeout( $Socket, $receiveTimeout ) )
    {
        return FALSE;
    }

    fwrite( $Socket, "\xFE" );
    $Data = fread( $Socket, 256 );
    fclose( $Socket );

    if( strlen( $Data ) < 4 || $Data[ 0 ] != "\xFF" )
    {
        return FALSE;
    }

    $Data = SubStr( $Data, 3 );
    $Data = iconv( 'UTF-16BE', 'UTF-8', $Data );
    $Data = Explode( "\xA7", $Data );

    return Array(
        'HostName'   => SubStr( $Data[ 0 ], 0, -1 ),
        'Players'    => isset( $Data[ 1 ] ) ? IntVal( $Data[ 1 ] ) : 0,
        'MaxPlayers' => isset( $Data[ 2 ] ) ? IntVal( $Data[ 2 ] ) : 0
    );

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文