Error[8]: Undefined offset: 1, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述PHP如何实现简单的Socket

PHP如何实现简单的Socket

首先使用PHP制作的Socket服务端,其主要是设置Socket的IP地址及端口号;然后监听端口,如果有客户端连接的话,则接收连接请求和数据,最后处理数据并且返回数据即可。

示例代码:

<?PHP//确保在连接客户端时不会超时set_time_limit(0);//设置IP和端口号$address = "127.0.0.1";$port = 2048; //调试的时候,可以多换端口来测试程序!/** * 创建一个SOCKET  * AF_INET=是ipv4 如果用ipv6,则参数为 AF_INET6 * SOCK_STREAM为socket的tcp类型,如果是UDP则使用SOCK_DGRAM*/$sock = socket_create(AF_INET, SOCK_STREAM, Sol_TCP) or dIE("socket_create() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n");//阻塞模式socket_set_block($sock) or dIE("socket_set_block() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n");//绑定到socket端口$result = socket_bind($sock, $address, $port) or dIE("socket_bind() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n");//开始监听$result = socket_Listen($sock, 4) or dIE("socket_Listen() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n");echo "OK\nBinding the socket on $address:$port ... ";echo "OK\nNow ready to accept connections.\nListening on the socket ... \n";do { // never stop the daemon //它接收连接请求并调用一个子连接Socket来处理客户端和服务器间的信息 $msgsock = socket_accept($sock) or dIE("socket_accept() Failed: reason: " . socket_strerror(socket_last_error()) . "/n");  //读取客户端数据 echo "Read clIEnt data \n"; //socket_read函数会一直读取客户端数据,直到遇见\n,\t或者[+++]字符.PHP脚本把这写字符看做是输入的结束符. $buf = socket_read($msgsock, 8192); echo "Received msg: $buf \n";  //数据传送 向客户端写入返回结果 $msg = "welcome \n"; socket_write($msgsock, $msg, strlen($msg)) or dIE("socket_write() Failed: reason: " . socket_strerror(socket_last_error()) ."/n"); //一旦输出被返回到客户端,父/子socket都应通过socket_close($msgsock)函数来终止 socket_close($msgsock);} while (true);socket_close($sock);

推荐教程:《PHP教程》 总结

以上是编程之家为你收集整理的PHP如何实现简单的Socket全部内容,希望文章能够帮你解决PHP如何实现简单的Socket所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
PHP如何实现简单的Socket_Golang_内存溢出

PHP如何实现简单的Socket

PHP如何实现简单的Socket,第1张

概述PHP如何实现简单的Socket

PHP如何实现简单的Socket

首先使用PHP制作的Socket服务端,其主要是设置Socket的IP地址及端口号;然后监听端口,如果有客户端连接的话,则接收连接请求和数据,最后处理数据并且返回数据即可。

示例代码:

<?PHP//确保在连接客户端时不会超时set_time_limit(0);//设置IP和端口号$address = "127.0.0.1";$port = 2048; //调试的时候,可以多换端口来测试程序!/** * 创建一个SOCKET  * AF_INET=是ipv4 如果用ipv6,则参数为 AF_INET6 * SOCK_STREAM为socket的tcp类型,如果是UDP则使用SOCK_DGRAM*/$sock = socket_create(AF_INET, SOCK_STREAM, Sol_TCP) or dIE("socket_create() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n");//阻塞模式socket_set_block($sock) or dIE("socket_set_block() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n");//绑定到socket端口$result = socket_bind($sock, $address, $port) or dIE("socket_bind() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n");//开始监听$result = socket_Listen($sock, 4) or dIE("socket_Listen() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n");echo "OK\nBinding the socket on $address:$port ... ";echo "OK\nNow ready to accept connections.\nListening on the socket ... \n";do { // never stop the daemon //它接收连接请求并调用一个子连接Socket来处理客户端和服务器间的信息 $msgsock = socket_accept($sock) or dIE("socket_accept() Failed: reason: " . socket_strerror(socket_last_error()) . "/n");  //读取客户端数据 echo "Read clIEnt data \n"; //socket_read函数会一直读取客户端数据,直到遇见\n,\t或者字符.PHP脚本把这写字符看做是输入的结束符. $buf = socket_read($msgsock, 8192); echo "Received msg: $buf \n";  //数据传送 向客户端写入返回结果 $msg = "welcome \n"; socket_write($msgsock, $msg, strlen($msg)) or dIE("socket_write() Failed: reason: " . socket_strerror(socket_last_error()) ."/n"); //一旦输出被返回到客户端,父/子socket都应通过socket_close($msgsock)函数来终止 socket_close($msgsock);} while (true);socket_close($sock);

推荐教程:《PHP教程》 总结

以上是编程之家为你收集整理的PHP如何实现简单的Socket全部内容,希望文章能够帮你解决PHP如何实现简单的Socket所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/997794.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存