服务端:

server.php

  1. <?php  
  2. $sendStr="我是服务端参数2";    
  3. $socket=socket_create(AF_INET,SOCK_STREAM,getprotobyname("tcp"));  //建立通道  
  4. socket_bind($socket,"localhost",1234);   //绑定要监听的端口  
  5. if(socket_listen($socket)){   //监听端口  
  6.     $receiveSocket=socket_create(AF_INET,SOCK_STREAM,getprotobyname("tcp"));     
  7.     $receiveSocket=socket_accept($socket);  
  8.     socket_write($receiveSocket,$sendStr,strlen($sendStr));   //写如通道  
  9.     $receiveStr="";  
  10.     $receiveStr=socket_read($receiveSocket,1024);  //读取通道  
  11.     echo "客户端传入:".$receiveStr;  
  12. }  
  13. socket_close($receiveSocket);  
  14. socket_close($socket);  
  15. ?>  


客户端:

client.php

  1. <?php  
  2.  $sendStr="客户端传递参数为1";  
  3.  $socket=socket_create(AF_INET,SOCK_STREAM,getprotobyname("tcp"));  //建立通道  
  4.    
  5.  if(socket_connect($socket,"localhost",1234)){  //连接通道  
  6.   $receiveStr="";  
  7.   $receiveStr=socket_read($socket,1024); //读取服务端返回参数  
  8.   echo "服务端返回参数:".$receiveStr;    
  9.   socket_write($socket,$sendStr,strlen($sendStr));  //参数写入通道中  
  10.  }  
  11.  socket_close($socket);   
  12. ?>  
先运行,server.php  再运行client.php

建议在命令行下运行该代码;


网页结果:

 

 

___________________________________________________________________

 

server:

复制代码
 1 <?php
 2 set_time_limit(0);
 3 
 4 $ip = '127.0.0.1';
 5 $port = 8888;
 6 
 7 // 1. 创建
 8 if( ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) == FALSE ){
 9     echo 'create fail:' . socket_strerror(socket_last_error());
10 }
11 
12 // 2. 绑定
13 if ( socket_bind($sock, $ip, $port) == FALSE ) {
14     echo 'bind fail:' . socket_strerror(socket_last_error());
15 }
16 
17 // 3. 监听
18 if( socket_listen($sock, 4) == FALSE ){
19     echo 'listen fail:' . socket_strerror(socket_last_error());
20 }
21 
22 $count = 0;
23 
24 do{
25     // 4. 阻塞,等待客户端请求
26     if ( ($msgsock = socket_accept($sock)) == FALSE ) {
27         
28         echo 'accept fail:' . socket_strerror(socket_last_error());
29         
30         break;
31     } else {
32 
33         // 5. 向客户端写入信息
34         $msg = 'server send successfully!';
35         socket_write($msgsock, $msg, strlen($msg));
36 
37         
38         // 5. 读取客户端信息
39         echo '-----test successfully!------';
40         $buf = socket_read($msgsock, 8192);
41 
42 
43         $talkback = 'receive client: ' . $buf;
44         echo $talkback;
45 
46 
47         if ($count >= 5) {
48             break;
49         }
50     }
51 
52     // 6. 关闭socket
53     socket_close($msgsock);
54 
55 }while(true);
56 
57 // 6. 关闭socket
58 socket_close($sock);
复制代码

 

 

client:

复制代码
<?php
error_reporting(E_ALL);
set_time_limit(0);


$ip = '127.0.0.1';
$port = 8888;

// 1. 创建
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if( $socket == FALSE ) {
    echo 'create fail: ' . socket_strerror(socket_last_error());
} else {
    echo 'OK';
}

// 2. 链接
echo 'we will try to connect ' . $ip .':' . $port . '\r\n----';
$result = socket_connect($socket, $ip, $port);
if ( $result == FALSE) {
    
}

$in = 'HO ';
$in .= 'first blood--------';
$out = '';

// 3. 向服务端写入
if( !socket_write($socket, $in, strlen($in)) ) {
    echo 'write fail: ' . socket_strerror(socket_last_error());
} else {
    echo '-----send to server succefully! \r\n----';
    echo 'the content is ' . $in;
}

// 3. 从服务端读取
while ( $out = socket_read($socket, 8129) ) {
    echo '-----receive from server succefully!\r\n------';
    echo 'the contents is ' . $out;
}

// 4. 关闭
echo '----close socket ...';
socket_close($socket);
echo 'closed ok.';
复制代码