简单的php —— socket 编程

简单的php————socket编程
服务端
server.php
<?php
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)or die('could not create socket');  //创建socket
socket_bind($socket,'127.0.0.1',1010)or die('could not bind to socket');   //为socket编定主机端口
socket_listen($socket);  
do{$connect = socket_accept($socket);  //等待客户端请求
   $str = socket_read($connect,4059);   //读出客户端请求字符串
   socket_write($connect,$str);        
   socket_close($connect);   //关闭
}while(true);
socket_close($socket);       //关闭
?>

客户端
client.php
<?php
  $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)or die('could not create socket');   //创建socket
  $connect = socket_connect($socket,'127.0.0.1',1010);  //创建链接
  socket_write($socket,'111111111111');   //向服务端写入
  $str = socket_read($socket,4059);      
  print_r($str);
?>

你可能感兴趣的:(编程,PHP,socket)