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 ?>
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);
// 设置两个变量 $host = "192.168.1.99" ; $port = 1234 ;
// 超时时间 set_time_limit ( 0 );
// 创建Socket $socket = socket_create ( AF_INET , SOCK_STREAM , 0 ) or die( "Could not create socket\n" );
// 创建 socket $socket = socket_create ( AF_INET , SOCK_DGRAM , 0 ) or die( "Could not create socket\n" );
// 绑定 socket to 指定地址和端口 $result = socket_bind ( $socket , $host , $port ) or die( "Could not bind to socket\n" );
// 开始监听连接 $result = socket_listen ( $socket , 3 ) or die( "Could not set up socket listener\n" );
//接受请求链接 // 调用子socket 处理信息 $spawn = socket_accept ( $socket ) or die( "Could not accept incoming connection\n" );
// 读取客户端输入 $input = socket_read ( $spawn , 1024 ) or die( "Could not read input\n" ); ?& gt ;
// 处理客户端输入并返回数据 $output = strrev ( $input ) . "\n" ; socket_write ( $spawn , $output , strlen ( $output )) or die( "Could not write output\n" );
// 关闭 sockets socket_close ( $spawn ); socket_close ( $socket );
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有