注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

BCB-DG's Blog

...

 
 
 

日志

 
 

PHP: socket_send  

2014-09-02 09:47:47|  分类: PHP |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

socket_send

(PHP 4 >= 4.1.0, PHP 5)

socket_sendSends data to a connected socket

说明

int socket_send ( resource $socket , string $buf , int $len , int $flags )

The function socket_send() sends len bytes to the socket socket from buf.

参数

socket

A valid socket resource created with socket_create() or socket_accept().

buf

A buffer containing the data that will be sent to the remote host.

len

The number of bytes that will be sent to the remote host from buf.

flags

The value of flags can be any combination of the following flags, joined with the binary OR (|) operator.

Possible values for flags
MSG_OOB Send OOB (out-of-band) data.
MSG_EOR Indicate a record mark. The sent data completes the record.
MSG_EOF Close the sender side of the socket and include an appropriate notification of this at the end of the sent data. The sent data completes the transaction.
MSG_DONTROUTE Bypass routing, use direct interface.

返回值

socket_send() returns the number of bytes sent, or FALSE on error.

参见

  • socket_sendto() - Sends a message to a socket, whether it is connected or not
add a note add a note

User Contributed Notes 2 notes

up
-1
jo at edarta dot be ?
6 years ago
In php-5.2.0, there's no MSG_EOF anymore. This is not even needed if you make sure you handle writes and close of sockets correctly.
up
-2
KingOfDos ?
8 years ago
I changed the Magic Packet function to this. I beleave that an function may not return any value, only an true/false (in the most way's).
<?PHP
function wol_magic_packet($mac,$addr='255.255.255.255') {
   
//Requirements__________________________
    //    You need to load the php_sockets.dll (in case of Windows, don't
    //    know @ linux, compile with --socket-support i beleave ).
    //    Otherwise he cannot find the socket_create function
    //Usage________________________________
    //    $addr:
    //        You will send and broadcast tho this addres.
    //        Normaly you need to use the 255.255.255.255 adres, so i made it as default. So you don't need
    //        to do anything with this.
    //    $mac:
    //        You will WAKE-UP this WOL-enabled computer, you need to add the MAC-addres here.
    //
    //Return________________________________
    //    TRUE:    When socked was created succesvolly and the message has been send.
    //    FALSE:    Something went wrong
    //
    //Example_1_____________________________
    //    When the message has been send you will see the message "Done...."
    //
    //    if ( wol_magic_packet ( '00:00:00:00:00:00' ) )
    //        echo 'Done...';
    //    else
    //        echo 'Error while sending';
    //
    //Example_2_____________________________
    //    To loop more computers:
    //
    //    $arr = array(
    //        '00:00:00:00:00:00',
    //        '00:00:00:00:00:00'
    //    );
    //    foreach($arr as $this_id => $this_mac)
    //        if (! wol_magic_packet ( $this_mac ))
    //            echo 'Error while sending to ['. $this_mac .']<br />'."\r\n";
    //___________________________________________
   
    //Check if it's an real MAC-addres and split it into an array
   
if (!preg_match("/([A-F0-9]{2}[-:]){5}[A-F0-9]{2}/",$mac,$maccheck))
        return
false;
   
$addr_byte = preg_split("/[-:]/",$maccheck[0]);
   
   
//Creating hardware adress
   
$hw_addr = '';
    for (
$a=0; $a < 6; $a++)//Changing mac adres from HEXEDECIMAL to DECIMAL
       
$hw_addr .= chr(hexdec($addr_byte[$a]));
   
   
//Create package data
   
$msg = str_repeat(chr(255),6);
    for (
$a = 1; $a <= 16; $a++)
       
$msg .= $hw_addr;

   
//Sending data
   
if ( //If
       
function_exists('socket_create') AND //socket_create exists
       
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) AND //Can create the socket
       
$sock_data = socket_connect($sock, $addr, 2050) //Can connect to the socket
   
) {  //Then
       
$sock_data = socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1); //Set
       
$sock_data = socket_write($sock, $msg, strlen($msg)); //Send data
       
socket_close($sock); //Close socket
       
return true;
    } else
//Esle? :P
       
return false;
}
?>
  评论这张
 
阅读(600)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017