php, php基础, Socket, php socket教程, WebSocket, PHP函数
后端开发-php教程
php socket(fsockopen)函数的应用实例软件管理工具箱源码,ubuntu创建qml应用,tomcat8压缩版插件,抓取书籍的爬虫,php电商网站设计与制作教材,aspcms源码seolzw
使用fsockopen函数,首先要开启php.ini中的allow_url_open=on;手机版个人商城源码下载,vscode保存PHP,UBUNTU系统b超,电脑远程tomcat,sqlite数据分页,limited网址服务器,移动图片缩放插件,前端的各种框架,web爬虫是什么意思,php画字,seo培训哪的好,旅游景点网站源码,三级分销系统网页版,仿博客风格模板,简单数据库管理系统页面,cms内容管理系统统计模板,带会员续费功能的网站程序lzw
fsockopen是对socket客户端代码的封装,该函数中封装了socket_create(),socket_connect()。excel web源码下载,vscode运行go项目,安装ubuntu很慢,tomcat自动补全url,sqlite指定数据查询,没有项目的爬虫工程师简历,php安全检测,濮阳网络seo推广优化,在线视频的网站源码,web个人网站模板lzw
服务器端代码:server.php<?phperror_reporting(E_ALL);set_time_limit(0);$address = "127.0.0.1";$port = 10008;//创建端口if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "socket_create() failed:reason:" . socket_strerror(socket_last_error()) . "\n";}//绑定if (socket_bind($sock, $address, $port) === false) { echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";}//监听if (socket_listen($sock, 5) === false) {echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";}while (true) {//得到一个链接if (($msgsock = socket_accept($sock)) === false) { echo "socket_accepty() failed :reason:".socket_strerror(socket_last_error($sock)) . "\n";break;}//welcome 发送到客户端$msg = "1.server send:welcome
";socket_write($msgsock, $msg, strlen($msg)); //返回信息给客户端echo "read client message\n";$buf = socket_read($msgsock, 8192); //获取客户端发送过来的信息$talkback = "2.received message:$buf\n";echo $talkback;if (false === socket_write($msgsock, $talkback, strlen($talkback))) { //返回信息给客户端 echo "socket_write() failed reason:" . socket_strerror(socket_last_error($sock)) ."\n";} else { echo "send success";}socket_close($msgsock);}socket_close($sock);
客户端代码:fsocket.php
<?php$fp = fsockopen("127.0.0.1", 10008, &$errno, &$errstr, 10);if (!$fp) {echo $errstr . " (". $errno . ")
n";} else {$in = "HEAD / http/1.1\r\n";$in .= "HOST: localhost \r\n";$in .= "Connection: close\r\n\r\n";fputs($fp, $in);while (!feof($fp)) {echo fgets($fp, 128);}fclose($fp);}?>