php socket编程

客户端代码:

 1 <?php
 2 error_reporting(E_ALL);
 3 set_time_limit(0);
 4 echo "<h2>TCP/IP Connection</h2>\n";
 5 
 6 $port = 1935;
 7 $ip = "127.0.0.1";
 8 
 9 /*
10  +-------------------------------
11  *    @socket连接整个过程
12  +-------------------------------
13  *    @socket_create
14  *    @socket_connect
15  *    @socket_write
16  *    @socket_read
17  *    @socket_close
18  +--------------------------------
19  */
20 
21 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
22 if ($socket < 0) {
23     echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
24 }else {
25     echo "OK.\n";
26 }
27 
28 echo "试图连接 '$ip' 端口 '$port'...\n";
29 $result = socket_connect($socket, $ip, $port);
30 if ($result < 0) {
31     echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
32 }else {
33     echo "连接OK\n";
34 }
35 
36 $in = "Ho\r\n";
37 $in .= "first blood\r\n";
38 $out = '';
39 
40 if(!socket_write($socket, $in, strlen($in))) {
41     echo "socket_write() failed: reason: " . socket_strerror($socket) . "\n";
42 }else {
43     echo "发送到服务器信息成功!\n";
44     echo "发送的内容为:<font color='red'>$in</font> <br>";
45 }
46 
47 while($out = socket_read($socket, 8192)) {
48     echo "接收服务器回传信息成功!\n";
49     echo "接受的内容为:",$out;
50 }
51 
52 
53 echo "关闭SOCKET...\n";
54 socket_close($socket);
55 echo "关闭OK\n";
56 ?>

 

服务器端代码

 1 <?php
 2 //确保在连接客户端时不会超时
 3 set_time_limit(0);
 4 
 5 $ip = '127.0.0.1';
 6 $port = 1935;
 7 
 8 /*
 9  +-------------------------------
10  *    @socket通信整个过程
11  +-------------------------------
12  *    @socket_create
13  *    @socket_bind
14  *    @socket_listen
15  *    @socket_accept
16  *    @socket_read
17  *    @socket_write
18  *    @socket_close
19  +--------------------------------
20  */
21 
22 /*----------------    以下操作都是手册上的    -------------------*/
23 if(($sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) {
24     echo "socket_create() 失败的原因是:".socket_strerror($sock)."\n";
25 }
26 
27 if(($ret = socket_bind($sock,$ip,$port)) < 0) {
28     echo "socket_bind() 失败的原因是:".socket_strerror($ret)."\n";
29 }
30 
31 if(($ret = socket_listen($sock,4)) < 0) {
32     echo "socket_listen() 失败的原因是:".socket_strerror($ret)."\n";
33 }
34 
35 $count = 0;
36 
37 do {
38     if (($msgsock = socket_accept($sock)) < 0) {
39         echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
40         echo "等待连接。。。。";
41         break;
42     } else {
43         
44         //发到客户端
45         $msg ="测试成功!\n";
46         socket_write($msgsock, $msg, strlen($msg));
47         
48         echo "测试成功了啊\n";
49         $buf = socket_read($msgsock,8192);
50         
51         
52         $talkback = "收到的信息:$buf\n";
53         echo $talkback;
54         
55         if(++$count >= 5){
56             break;
57         };
58         
59     
60     }
61     //echo $buf;
62     socket_close($msgsock);
63 
64 } while (true);
65 
66 socket_close($sock);
67 ?>

 

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