我有套接字在我的应用程序的几个页面上查询游戏服务器。我有一个问题,当我在页面之间导航太快时,套接字没有正确关闭,我得到的错误是这样的;
socket_recv() [function.socket-recv]: unable to read from socket [104]: Connection reset by peer
在每个页面完全加载后,我必须等待大约 4-5 秒才能关闭。
这是套接字的代码;
function QueryMinecraft( $IP, $Port, $Timeout =1 )
{
$Socket = Socket_Create( AF_INET, SOCK_STREAM, SOL_TCP );
Socket_Set_Option( $Socket, SOL_SOCKET, SO_SNDTIMEO, array( 'sec' => (int)$Timeout, 'usec' => 0 ) );
if( $Socket === FALSE || @Socket_Connect( $Socket, $IP, (int)$Port ) === FALSE )
{
Socket_Close( $Socket );
return FALSE;
} else {
Socket_Close( $Socket );
return TRUE;
}
Socket_Close( $Socket );
}
这是另一页上套接字的代码;
function QueryMinecraft( $IP, $Port, $Timeout = 3 )
{
$Socket = Socket_Create( AF_INET, SOCK_STREAM, SOL_TCP );
Socket_Set_Option( $Socket, SOL_SOCKET, SO_SNDTIMEO, array( 'sec' => (int)$Timeout, 'usec' => 0 ) );
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 );
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
);
}
谢谢!