电脑安全

您当前的位置:主页 > 电脑安全 >

PHP socket,server,cient,模拟post-网络编程

来源:[db:来源]标题:   宋体    编辑:电脑技术全能网 时间:2019-09-23 06:23

原标题:PHP socket,server,cient,模拟post-网络编程
   fsockopen以socket方法翻开一个衔接我最罕用的是模仿post这个是小李发掘的到的...不错至于要模仿get方法....间接file_get_content就行了.代码见前面stream_socket_client, 代码见前面这个和fsockopenfsockopen一样.以socket方法翻开一个衔接,只是参数差别stream_socket_server树立一个socket server端, 代码见前面假如是树立的是tcp的server 就用stream_socket_accept停止通信假如是树立的是udp的server 就用stream_socket_recvfrom和stream_socket_sendto停止通信,并且stream_socket_server中须要加个参数STREAM_SERVER_BIND另有个socket扩大,这个是最基本的树立socket,然而从5.3.0开端就放到pecl中了.这里就不先容了.---------------------------- stream_socket client---------------------------<?php/*** @name test.php* @date Sun Jan 27 00:49:00 CST 2008* @copyright 马永占(MyZ)* @author 马永占(MyZ) * @link http://blog.111cn.net/mayongzhan/*/$xport = "tcp";$port = "8001";$ip = "127.0.0.1";$address = "{$xport}://{$ip}:{$port}";$fp = stream_socket_client($address, $errno, $errstr, 1);if (!$fp) { echo "$errstr ($errno)<br /> ";} else { fwrite($fp, " "); echo fread($fp, 1024); fclose($fp);}?>---------------------------- streamSocketServer---------------------------<?php/*** @name test2.php* @date Sun Jan 27 00:45:57 CST 2008* @copyright 马永占(MyZ)* @author 马永占(MyZ) * @link http://blog.111cn.net/mayongzhan/*/header("Content-type: text/html;charset=utf-8");//设置不超时.效劳端固然不能超时set_time_limit(0);//失掉可用socket$xportlist = stream_get_transports();echo "transports: ";foreach ($xportlist as $value) { echo "{$value} ";}//界说一些货色$xport = "tcp";$port = "8001";$address = "{$xport}://0.0.0.0:{$port}";//树立socketserverif ($xport==''tcp'') { $socket = stream_socket_server($address, $errno, $errstr);}elseif ($xport==''udp'') { $socket = stream_socket_server($address, $errno, $errstr, STREAM_SERVER_BIND);}if (!$socket) { echo "{$errstr} ({$errno}) ";}else { echo "listening {$xport}:{$port} ... "; if ($xport==''tcp'') { //允许一个socket衔接,-1超时 while ($conn = stream_socket_accept($socket,-1)) { //失掉拜访的端口 $peer = stream_socket_get_name($conn,true); echo "$peer "; fwrite($conn, ''The local time is '' . date("Y-m-d H:i:s ")); fclose($conn); } } elseif ($xport==''udp'') { do { //失掉拜访的端口 $pkt = stream_socket_recvfrom($socket, 1, 0, $peer); echo "$peer "; stream_socket_sendto($socket, date("Y-m-d H:i:s "), 0, $peer); } while ($pkt !== false); } //封闭socket fclose($socket);}?>

上一篇:php 遍历目录-网络编程

下一篇:没有了