服务端:
server.php
<?php
$sendStr="我是服务端参数2";
$socket=socket_create(AF_INET,SOCK_STREAM,getprotobyname("tcp")); //建立通道
socket_bind($socket,"localhost",1234); //绑定要监听的端口
if(socket_listen($socket)){ //监听端口
$receiveSocket=socket_create(AF_INET,SOCK_STREAM,getprotobyname("tcp"));
$receiveSocket=socket_accept($socket);
socket_write($receiveSocket,$sendStr,strlen($sendStr)); //写如通道
$receiveStr="";
$receiveStr=socket_read($receiveSocket,1024); //读取通道
echo "客户端传入:".$receiveStr;
}
socket_close($receiveSocket);
socket_close($socket);
?>
客户端:
client.php
<?php先运行,server.php 再运行client.php
$sendStr="客户端传递参数为1";
$socket=socket_create(AF_INET,SOCK_STREAM,getprotobyname("tcp")); //建立通道
if(socket_connect($socket,"localhost",1234)){ //连接通道
$receiveStr="";
$receiveStr=socket_read($socket,1024); //读取服务端返回参数
echo "服务端返回参数:".$receiveStr;
socket_write($socket,$sendStr,strlen($sendStr)); //参数写入通道中
}
socket_close($socket);
?>
建议在命令行下运行该代码;
网页结果: