- <?php
- /**
- * <p>sockets连接(UDP)</p>
- */
- class sockets {
- public $host;//连接服务地址
- public $part;//端口
- private $limitTime = 0 ; // 连接超时时间
- private $socket;
- // 构造函数
- public function __construct(){
- //设置超时时间
- set_time_limit($this->limitTime);
- //创建socket
- $this->socket = socket_create(AF_INET, SOCK_DGRAM, 0);//upd:SOCK_DGRAM tcp:SOCK_STREAM
- }
- //发送消息
- public function sendMsg($msg){
- if($this->socket){
- //绑定ip和端口
- $result = socket_bind($this->socket, $this->host, $this->port);
- if($result ){
- socket_write($this->socket,$msg,strlen($msg));
- $r = socket_read($this->socket,100);
- }
- }
- }
- // 析构函数
- public function __destruct(){
- if($this->socket){
- //关闭socket
- socket_close($this ->socket);
- }
- }
- }
- ?>