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 break; 41 } else { 42 43 //发到客户端 44 $msg ="测试成功!\n"; 45 socket_write($msgsock, $msg, strlen($msg)); 46 47 echo "测试成功了啊\n"; 48 $buf = socket_read($msgsock,8192); 49 50 51 $talkback = "收到的信息:$buf\n"; 52 echo $talkback; 53 54 if(++$count >= 5){ 55 break; 56 }; 57 58 59 } 60 //echo $buf; 61 socket_close($msgsock); 62 63 } while (true); 64 65 socket_close($sock); 66 ?>
这是socket的服务端代码。然后运行cmd,注意是自己的程序存放路径啊。
没有反映,对现在服务端的程序已经开始运行,端口已经开始监听了。运行netstat -ano可以查看端口情况,我的是1935端口
看,端口已经处于LISTENING状态了。接下来我们只要运行客户端程序即可连接上。上代码
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 ?>
至此客户端已经连接上服务端了。
案例二:代码详解
// 设置一些基本的变量 $host = "192.168.1.99"; $port = 1234; // 设置超时时间 set_time_limit(0); // 创建一个Socket $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not createsocket\n"); //绑定Socket到端口 $result = socket_bind($socket, $host, $port) or die("Could not bind tosocket\n"); // 开始监听链接 $result = socket_listen($socket, 3) or die("Could not set up socketlistener\n"); // accept incoming connections // 另一个Socket来处理通信 $spawn = socket_accept($socket) or die("Could not accept incomingconnection\n"); // 获得客户端的输入 $input = socket_read($spawn, 1024) or die("Could not read input\n"); // 清空输入字符串 $input = trim($input); //处理客户端输入并返回结果 $output = strrev($input) . "\n"; socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n"); // 关闭sockets socket_close($spawn); socket_close($socket);
下面是其每一步骤的详细说明: