PHP Socket 简单使用

/*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 简单使用)