C语言 c++ php mysql nginx linux lnmp lamp lanmp memcache redis 面试 笔记 ppt 设计模式 问题 远程连接

php socket 学习

 

socket超时设置

ini_set("default_socket_timeout", -1);

 

stream_set_timeout

$fp = fsockopen("www.example.com", 80);
if (!$fp) {
    echo "Unable to open\n";
} else {

    fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
    stream_set_timeout($fp, 2);
    $res = fread($fp, 2000);

    $info = stream_get_meta_data($fp);
    fclose($fp);

    if ($info['timed_out']) {
        echo 'Connection timed out!';
    } else {
        echo $res;
    }

}

注意:

1、This function doesn't work with advanced operations like stream_socket_recvfrom(), use stream_select() with timeout parameter instead. 

 

posted on 2015-04-30 23:49  思齐_  阅读(336)  评论(0编辑  收藏  举报