php fsockopen ÖжàÏ̵߳Ľâ¾ö°ì·¨
×î½üÑо¿php¶àÏ̵߳ÄÎÊÌ⣬·¢ÏÖÖÐÎÄ×ÊÔ´ÉٵĿÉÁ¯£¬½öÓеļ¸ÆªÎÄÕ±»×ªÁËÓÖת£¬µ«ÎÄÖÐÄÚÈݼÛÖµÓÐÏÞ¡£ËÑË÷¹ý³ÌÖз¢ÏÖ¹úÍâºÜ¶àÍøÕ¾ÒýÓõÄһƪÎÄÕÂд µÄ²»´í£¬ËùÒÔ·Òë¹ýÀ´¡£
°æȨÉùÃ÷£º¿ÉÒÔÈÎÒâתÔØ£¬×ªÔØʱÇëÎñ±ØÒÔ³¬Á´½ÓÐÎʽ±êÃ÷ÎÄÕÂÔʼ³ö´¦ºÍ×÷ÕßÐÅÏ¢¼°±¾ÉùÃ÷
http://blog.iyi.cn/start/
ÎÊÌ⣺
ÓÐûÓа취ÔÚphpÖÐʵÏÖ¶àÏß³ÌÄØ£¿
¼ÙÉèÄãÕýÔÚдһ¸ö»ùÓÚ¶ą̀·þÎñÆ÷µÄphpÓ¦Óã¬ÀíÏëµÄÇé¿öʱͬʱÏò¶ą̀·þÎñÆ÷·¢ËÍÇëÇ󣬶ø²»ÊÇһ̨½Óһ̨¡£
¿ÉÒÔʵÏÖÂð£¿
»Ø´ð£º
µ±ÓÐÈËÏëҪʵÏÖ²¢·¢¹¦ÄÜʱ£¬ËûÃÇͨ³£»áÏëµ½ÓÃfork»òÕßspawn threads£¬µ«Êǵ±ËûÃÇ·¢ÏÖphp²»Ö§³Ö¶àÏ̵߳Äʱºò£¬´ó¸Å»áת»»Ë¼Â·È¥ÓÃһЩ²»¹»ºÃµÄÓïÑÔ£¬±ÈÈçperl¡£
ÆäʵµÄÊÇ´ó¶àÊýÇé¿öÏ£¬Äã´ó¿É²»±ØʹÓÃfork»òÕßỊ̈߳¬²¢ÇÒÄã»áµÃµ½±ÈÓÃfork»òthread¸üºÃµÄÐÔÄÜ¡£
¼ÙÉèÄãÒª½¨Á¢Ò»¸ö·þÎñÀ´¼ì²éÕýÔÚÔËÐеÄn̨·þÎñÆ÷£¬ÒÔÈ·¶¨ËûÃÇ»¹ÔÚÕý³£ÔËת¡£Äã¿ÉÄÜ»áдÏÂÃæÕâÑùµÄ´úÂ룺
<?php $hosts = array("host1.sample.com", "host2.sample.com", "host3.sample.com"); $timeout = 15; $status = array(); foreach ($hosts as $host) { $errno = 0; $errstr = ""; $s = fsockopen($host, 80, $errno, $errstr, $timeout); if ($s) { $status[$host] = "Connectedn"; fwrite($s, "HEAD / HTTP/1.0rnHost: $hostrnrn"); do { $data = fread($s, 8192); if (strlen($data) == 0) { break; } $status[$host] .= $data; } while (true); fclose($s); } else { $status[$host] = "Connection failed: $errno $errstrn"; } } print_r($status); ?>
ËüÔËÐеĺܺ㬵«ÊÇÔÚfsockopen()·ÖÎöÍêhostname²¢ÇÒ½¨Á¢Ò»¸ö³É¹¦µÄÁ¬½Ó£¨»òÕßÑÓʱ$timeoutÃ룩֮ǰ£¬À©³äÕâ¶Î´úÂëÀ´¹Ü Àí´óÁ¿·þÎñÆ÷½«ºÄ·ÑºÜ³¤Ê±¼ä¡£
Òò´ËÎÒÃDZØÐë·ÅÆúÕâ¶Î´úÂ룻ÎÒÃÇ¿ÉÒÔ½¨Á¢Òì²½Á¬½Ó-²»ÐèÒªµÈ´ýfsockopen·µ»ØÁ¬½Ó״̬¡£PHPÈÔÈ»ÐèÒª½âÎöhostname£¨ËùÒÔÖ±½ÓʹÓÃip¸ü ¼ÓÃ÷ÖÇ£©£¬²»¹ý½«ÔÚ´ò¿ªÒ»¸öÁ¬½ÓÖ®ºóÁ¢¿Ì·µ»Ø£¬¼Ì¶øÎÒÃǾͿÉÒÔÁ¬½ÓÏÂһ̨·þÎñÆ÷¡£
ÓÐÁ½ÖÖ·½·¨¿ÉÒÔʵÏÖ£»PHP5ÖпÉÒÔʹÓÃÐÂÔöµÄstream_socket_client()º¯ÊýÖ±½ÓÌæ»»µôfsocketopen()¡£PHP5֮ǰµÄ °æ±¾£¬ÄãÐèÒª×Ô¼º¶¯ÊÖ£¬ÓÃsocketsÀ©Õ¹½â¾öÎÊÌâ¡£
ÏÂÃæÊÇPHP5ÖеĽâ¾ö·½·¨£º
<?php $hosts = array("host1.sample.com", "host2.sample.com", "host3.sample.com"); $timeout = 15; $status = array(); $sockets = array(); foreach ($hosts as $id => $host) { $s = stream_socket_client("$host:80", $errno, $errstr, $timeout,STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT); if ($s) { $sockets[$id] = $s; $status[$id] = "in progress"; } else { $status[$id] = "failed, $errno $errstr"; } } while (count($sockets)) { $read = $write = $sockets; $n = stream_select($read, $write, $e = null, $timeout); if ($n > 0) { foreach ($read as $r) { $id = array_search($r, $sockets); $data = fread($r, 8192); if (strlen($data) == 0) { if ($status[$id] == "in progress") { $status[$id] = "failed to connect"; } fclose($r); unset($sockets[$id]); } else { $status[$id] .= $data; } } foreach ($write as $w) { $id = array_search($w, $sockets); fwrite($w, "HEAD / HTTP/1.0rnHost: " . $hosts[$id] . "rnrn"); $status[$id] = "waiting for response"; } } else { foreach ($sockets as $id => $s) { $status[$id] = "timed out " . $status[$id]; } break; } } foreach ($hosts as $id => $host) { echo "Host: $hostn"; echo "Status: " . $status[$id] . "nn"; } ?>
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ