NOTE! If you are trying to send a broadcast-message using this code you _may_ get a "Permission denied"-Error at socket_connect, even if you are running this as root on a linux box.<?php$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);socket_connect($sock,"255.255.255.255", 10000);socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1);$buf = "Hello World!";socket_write($sock,$buf,strlen($buf));socket_close($sock);?>The only workaround for this is to get the broadcast address of the interface and walk through all IPs with a for-loop.

Planning on sending integer values through as socket, I was surprised to find PHP only supports sending strings. I came to the conclusion the only way to do it would be to create a string that would evaluate to the same byte values as the integer I wanted to send. So (after much messing about) I created a couple of functions: one to create this 'string' and one to convert a received value back to an integer.

I've been using the ICMP Checksum calculation function written by Khaless [at] bigpond [dot] com. But when having an odd length of data, it failed, so I made my own instead, which adds a 0 if the data length is odd:<?phpfunction icmpChecksum($data){// Add a 0 to the end of the data, if it's an "odd length"if (strlen($data)%2)$data .= "\x00";

I spent a while trying to use SOCK_RAW to send ICMP request packets so i could ping. This however lead me to need the internet checksum written as a php function, which was a little hard because of the way PHP handles variable types. Anyway, to save others the effort heres what i came up with, this returns Checksum for $data

<?phpfunction wake_on_lan($mac,$addr=false,$port=7) {//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. // Since 255.255.255.255 have permission denied problems you can use addr=false to get all broadcast address from ifconfig command // addr can be array with broadcast IP values // $mac: // You will WAKE-UP this WOL-enabled computer, you need to add the MAC-addres here. // Mac can be array too // //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 ( wake_on_lan('00:00:00:00:00:00')) // echo 'Done...'; // else // echo 'Error while sending'; //if ($addr===false){exec("ifconfig | grep Bcast | cut -d \":\" -f 3 | cut -d \" \" -f 1",$addr);$addr=array_flip(array_flip($addr)); } if(is_array($addr)){$last_ret=false; for ($i=0;$i<count($ret);$i++) if ($ret[$i]!==false)$last_ret=wake_on_lan($mac,$ret[$i],$port); return($last_ret); } if (is_array($mac)){$ret=array(); foreach($mac as $k=>v)$ret[$k]=wake_on_lan($v,$addr,$port); return($ret); }//Check if it's an real MAC-addres and split it into an array$mac=strtoupper($mac); if (!preg_match("/([A-F0-9]{1,2}[-:]){5}[A-F0-9]{1,2}/",$mac,$maccheck)) return false;$addr_byte = preg_split("/[-:]/",$maccheck[0]);

There is a fantastic book on this library called 'TCP/IP Sockets in C' (ISBN 1558608265), that covers all of the ins and outs, quirks, and everything else that goes on. It's written for C, of course, but it could have easily been written for PHP with almost no serious code differences.

I have spent the past two days ripping out hair trying to figure out how to prevent zombie processes w/the examples above and I just happend to find this in the manual for another lanuage, felt it neccassry to port it here.

It's about being able to use multiple connectionsin a php socket server, WITHOUT havingto use those threads everyone seems to beso very fond of.works very well :)(ps: i didn't make it, so....don't say thanks to me ;),thank him)

"Beej's Guide to Network Programming" is an absolutely excellent and easy to understand tutorial to socket programming. It was written for C developers, but as the socket functions in PHP are (almost) analoguous, this should not be a problem.

After several hours of working with sockets in an attempt to do UDP broadcasting, I thought a little help was in order for anyone else looking to do something similar, since it uses a number of those "undocumented" functions. Here's how I did it:

<?php
// here is a basic opening of the a socket. AF_INET specifies the internet domain. SOCK_DGRAM
// specifies the Datagram socket type the 0 specifies that I want to use the default protcol (which in this
// case is UDP)
$sock = socket(AF_INET, SOCK_DGRAM, 0);

// here's where I set the socket options, this is essential to allow broadcasting. An earlier comment (as of
// June 4th, 2001) explains what the parameters are. For my purposes (UDP broadcasting) I need to set
// the broadcast option at the socket level to true. In C, this done using SOL_SOCKET as the level param
// (2) and SO_BROADCAST as the type param (3). These may exist in PHP but I couldn't reference them
// so I used the values that referencing these variables in C returns (namely 1 and 6 respectively). This
// function is basically just a wrapper to the C function so check out the C documentation for more info
$opt_ret = setsockopt($sock, 1, 6, TRUE);

// finally I am ready to broad cast something. The sendto function allows this without any
// connections (essential for broadcasting). So, this function sends the contents of $broadcast_string to the
// general broadcast address (255.255.255.255) on port 4096. The 0 (param 4) specifies no special
// options, you can read about the options with man sendto
$send_ret = sendto($sock, $broadcast_string, strlen($broadcast_string), 0, '255.255.255.255', 4096);

Here's a simple script for sending messages back and forth between a server and client. At this point, the code is fairly rough because once it enters the while loop, it doesn't stop but it can be modified and fixed. Enjoy.

function Build_Packet() {$data = "abcdefghijklmnopqrstuvwabcdefghi"; // the actual test data$type = "\x08"; // 8 echo message; 0 echo reply message$code = "\x00"; // always 0 for this program$chksm = "\x00\x00"; // generate checksum for icmp request$id = "\x00\x00"; // we will have to work with this later$sqn = "\x00\x00"; // we will have to work with this later

// now we need to change the checksum to the real checksum$chksm = $this->ip_checksum($type.$code.$chksm.$id.$sqn.$data);