½ºÆ®¸² ¼ÒÄÏ »óÅ ¾Ë¾Æº¸±â
## stream socket resource state, such as fsockopen(), stream_socket_client()
##
## return value
## -1 : $handle is empty or is not resource or unknown
## 0 : $handle is not ESTABLISHED
## 1 : $handle is ESTABLISHED
## misc
## @file_exists('/proc/net/tcp') ? define('_PROCTCP','/proc/net/tcp') : '';
##
function stream_socket_state(&$handle, $inode=NULL)
{
global $__proctcpfp; // self fp of /proc/net/tcp
if(!defined('_PROCTCP')) return -1;
if(!$handle || !@is_resource($handle)) return -1;
if(!is_resource($__proctcpfp) && !$__proctcpfp = fopen(_PROCTCP,'r')) return -1;
if(!$inode)
{
$fstat = @fstat($handle);
$inode = $fstat['ino'];
if($inode === FALSE) return -1;
}
rewind($__proctcpfp);
@fgets($__proctcpfp); // firest line to delete
while(!feof($__proctcpfp)) // fscanf() best speed
{
if(fscanf($__proctcpfp,"%d: %s %s %X %s %s %s %d %d %d %x %s %64s\n",
$null,$null,$null,$st,$null,$null,$null,$null,$null,$ino,$null,$null,$null)
< 13)
continue;
if($inode == $ino)
{
$st = hexdec($st); // 1 == ESTABLISHED
break;
}
}
//fclose($__proctcpfp);
return ($st===1) ? 1 : FALSE;
}
## example
@file_exists('/proc/net/tcp') ? define('_PROCTCP','/proc/net/tcp') : '';
$fp = fsockopen(....);
$state = stream_socket_state($fp);
echo $state;
fclose($fp);
|