php 之fsockopen(转)

1.函数作用

  打开网络的socket连接

2.使用参数

  int fsockopen(string hostname, int port, int [errno], string [errstr], int [timeout]);

3.简介

目前这个函数提供二个 Socket 资料流界面,分别为 Internet 用的 AF_INET 及 Unix 用的 AF_UNIX。
当在 Internet 情形下使用时,参数 hostname 及 port 分别代表网址及埠号。在 UNIX 情形可做 IPC,hostname 参数表示到 socket 的路径,port 配置为 0。
可省略的 timeout 选项表示多久没有连上就中断。
在使用本函数之后会返回文件指针,供文件函数使用,包括 fgets()、fgetss()、fputs()、fclose() 与 feof()。参数 errno 及 errstr 也是可省略的,主要当做错误处理使用。
使用本函数,会使用搁置模式 (blocking mode) 处理,可用 set_socket_blocking() 转换成无搁置模式
 
4.使用范例
本例用来模拟成 HTTP 连接。
<?php
$fp = fsockopen("191.168.100.100", 80, &$errno, &$errstr, 10);
if(!$fp)
{
echo "$errstr ($errno)<br>\n";
}
else {
fputs($fp,"GET / HTTP/1.0\nHost: 191.168.100.100\n\n");
while(!feof($fp)) {
echo fgets($fp,128);
}
fclose($fp);
} ?>

posted on 2013-03-17 20:39  李乐已存在  阅读(194)  评论(0编辑  收藏  举报

AmazingCounters.com