php 实现udp发送数据,主要使用 socket_sendto就可以了。

  $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  //PHP UDP socket_ recv() timeout
  socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>2,"usec"=>0));

  //Set socket options.
  socket_set_nonblock($socket);
  socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
  socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
  if (defined('SO_REUSEPORT'))
      socket_set_option($socket, SOL_SOCKET, SO_REUSEPORT, 1);

  //Bind to any address & rand port 
  socket_bind($socket, '0.0.0.0', 9999)
  
  $senddata = "hello";
  socket_sendto($socket,$sendcmd, strlen($senddata), 0, "127.0.0.1", 54321);

php 设置socket 非阻塞

socket_set_nonblock($socket);

php 设置udp 广播

socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);

php 设置 udp 地址端口重用

socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);

php 设置udp_recv接收超时

  socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>2,"usec"=>0));

php 字符串转大写

uppercase

php 判断http post

 function is_post()
{
    return isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD'])=='POST';    
}

php 取随机数

echo(rand();
echo(rand();
echo(rand(10,100))

php socket 官方链接地址
https://www.php.net/manual/en/book.sockets.php

php explode 示例

$arr = explode(":", $buf);

php trim 示例 

$str = "Hello Const!";
echo $str . "<br>";
echo trim($str,"Const!");
本文链接地址:https://const.net.cn/37.html

标签: udp, php, socket

添加新评论