PHP: socket_send - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

socket_sendto socket_select

Last updated: Fri, 26 Jan 2007

view this page in

# socket_send

(PHP 4 >= 4.1.0, PHP 5)

socket_send -- Sends data to a connected socket

## Description

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

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

The value of `flags` can be any `ORed` combination of the following:

Таблица 1. possible values for `flags`

`0x1` | Process OOB (out-of-band) data
`0x2` | Peek at incoming message
`0x4` | Bypass routing, use direct interface
`0x8` | Data completes record
`0x100` | Data completes transaction

See also socket_sendmsg() and socket_sendto().

add a note User Contributed Notes
socket_send
KingOfDos
08-Nov-2005 07:41
`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;
}
?>`
hotkey at spr dot at
18-Nov-2002 10:37
`Hi guys!
This is my example for socket_create, _connect and _send: It is a Wake On Lan (WOL) function to power-up a Computer with given MAC-Adress.
greetz!
//HotKey
- - - - - - - - - >8 - - - cut here! - - - - - -
function WakeOnLan($addr, $mac) { # (C)opyright 2K2 hotkey@spr.at
$addr_byte = explode(':', $mac);
$hw_addr = '';
for ($a=0; $a < 6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
# send it to the broadcast address using UDP
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false) {
echo "Error creating socket!\n";
echo "Error:".socket_strerror(socket_last_error($s));
} else {
$e = socket_connect($s, $addr, 2050);
if ($e == false) {
echo "connection failed\n";
echo "Error:".socket_strerror(socket_last_error($s));
} else {
$e = socket_setopt($s, SOL_SOCKET, SO_BROADCAST, 1);
$e = socket_write($s, $msg, strlen($msg));
socket_close($s);
echo "Magic Packet sent (".$e.") to ".$addr.", MAC=".$mac;
}
}
}
WakeOnLan('255.255.255.255', 'MAC:MAC:MAC:MAC:MAC:MAC'); #put the MAC-to-be-woken here!
# IP must be 255.255.255.255 - at least it didnt work otherwise...`
add a note
socket_sendto socket_select

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST