php 中操作tcp 与SOCKET函数实例 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
-
- $c=stream_socket_server("tcp://127.0.0.1:1100");
- while(1)
- {
- if(is_resource($a=stream_socket_accept($c)))
- {
-
-
- if(stream_socket_get_name($a,true)!==false)
- {
- fwrite($a,"regards form berlin");
- }
- }
- }
-
-
-
-
- $sockets=stream_socket_pair(stream_pf_unix,stream_sock_stream,stream_ipproto_ip);
-
- $pid=pcntl_fork();
- if($pid==-1)
- {
-
- die('could not fork');
- }
- else if($pid)
- {
-
- fclose($sockets[0]);
- fwrite($sockets[1], "child pid: $pidn");
- echo fgets($sockets[1]);
- fclose($sockets[1]);
- }
- else
- {
-
- fclose($sockets[1]);
- fwrite($sockets[0], "message from childn");
- echo fgets($sockets[0]);
- fclose($sockets[0]);
- }
-
-
-
-
- $server=stream_socket_server('tcp://127.0.0.1:1234');
-
- $socket=stream_socket_accept($server);
-
- echo "received out-of-band:'".stream_socket_recvfrom($socket,1500,stream_oob)."'n";
-
- echo "data:'".stream_socket_recvfrom($socket,1500,stream_peek)."'n";
-
- echo "data:'".stream_socket_recvfrom($socket,1500)."'n";
-
- fclose($socket);
- fclose($server);
-
-
-
-
- $socket=stream_socket_client('tcp://127.0.0.1:1234');
-
- fwrite($socket,"normal data transmit.");
-
- stream_socket_sendto($socket,"out of band data.",stream_oob);
-
- fclose($socket);
-
-
-
-
- $socket=stream_socket_server("tcp://0.0.0.0:8000",$errno,$errstr);
-
- if(!$socket)
- {
- echo "$errstr ($errno)<br />n";
- }
-
- else
- {
-
- while($conn=stream_socket_accept($socket))
- {
-
- fwrite($conn,'the local time is '.date('n/j/y g:i a')."n");
-
- fclose($conn);
- }
-
- fclose($socket);
- }
|
|
|
|