function SendUdpMsg($service_port, $address, $msg){
// 采用php socket技术使用UDP协议连接设备
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
@socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array(
"sec" => 2,
"usec" => 0
));
// 绑定IP/PORT
socket_bind($socket, '192.168.1.105', 8000);
// 发送命令
socket_sendto($socket, $msg, strlen($msg), 0, $address, $service_port);
// if (($ret = socket_listen($socket, 5)) < 0) {
// echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
// exit;
// }
if (false !== ($bytes = socket_recv($socket, $buf, 1024, MSG_PEEK))) {
echo "Read $bytes bytes from socket_recv(). Closing socket...";
} else {
echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
// 关闭连接
socket_close($socket);
return $buf;
}
正在学习Go语言的PHP程序员。