March 14, 2008
Socket To Me, One More Time
Sorry, I couldn’t resist. I’ve been testing receiving data through the socket server I talked about in my last post. If you are testing without a steady stream of incoming data, you may need to generate your own UDP packets to send to your socket server. Here’s a simple script to open the socket on the other end. Keep in mind there is no back and forth communication with the User Datagram Protocol (UDP).
Create the socket, see the PHP documentation on which settings to use, these are for a UDP connection.
#!/usr/bin/php -q <?php /* create a socket in the AF_INET family, using SOCK_DGRAM for udp connections */ $mysock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
Specify your host and port, and put together a packet string to send:
$host = "127.0.0.1"; //host to open socket on $port = 1228; //port number //our packet contents $buf = "packet data, comma, separated, to send, over there.";
Send your packet to the socket, specifying the length of the packet and the destination host and port.
socket_sendto($mysock, $buf, strlen($buf), 0x100, $host, $port); socket_close($mysock); ?>
