时间:2021-07-01 10:21:17 帮助过:20人阅读
<?php /*socket收发数据 @host(string) socket服务器IP @post(int) 端口 @str(string) 要发送的数据 @back 1|0 socket端是否有数据返回 返回true|false|服务端数据 */ function sendSocketMsg($host,$port,$str,$back=0){ echo '**************************'; $socket = socket_create(AF_INET,SOCK_STREAM,0); if ($socket < 0) return false; $result = @socket_connect($socket,$host,$port); if ($result == false)return false; socket_write($socket,$str,strlen($str)); socket_shutdown($socket); if($back!=0){ $input = socket_read($socket,5000); socket_close ($socket); return $input; }else{ socket_close ($socket); return true; }} $host = '192.168.1.100'; $post = 12299; $str = '{"@type":"xxx","args":[{"@type":"xxx","userInfo":{"nickname":"nickname", "phoneNo":"123143345345","roleType":"USER"}}],"methodName":"registerUser","serviceInterface":"xxx"}'; $back = 1; echo sendSocketMsg($host,$post,$str,$back);
相关推荐:
PHP中Socket服务器搭建和测试方法教程
实例详解微信小程序如何使用Socket
PHP 结合实例认识什么是Socket
以上就是PHP中Socket简单使用方法的详细内容,更多请关注Gxl网其它相关文章!