note 43057 added to function.socket-create
| From: | david at eder dot us | Date: | Tue, 08 Jun 2004 12:07:52 +0000 |
| Subject: | note 43057 added to function.socket-create | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-70486@lists.php.net to get a copy of this message | ||
Seems there aren't any examples of UDP clients out there. This is a tftp client. I hope this
makes someone's life easier.
<?php
function tftp_fetch($host, $filename)
{
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
// create the request packet
$packet = chr(0) . chr(1) . $filename . chr(0) . 'octet' . chr(0);
// UDP is connectionless, so we just send on it.
socket_sendto($socket, $packet, strlen($packet), 0x100, $host, 69);
$buffer = '';
$port = '';
$ret = '';
do
{
// $buffer and $port both come back with information for the ack
// 516 = 4 bytes for the header + 512 bytes of data
socket_recvfrom($socket, $buffer, 516, 0, $host, $port);
// add the block number from the data packet to the ack packet
$packet = chr(0) . chr(4) . substr($buffer, 2, 2);
// send ack
socket_sendto($socket, $packet, strlen($packet), 0, $host, $port);
// append the data to the return variable
// for large files this function should take a file handle as an arg
$ret .= substr($buffer, 4);
}
while(strlen($buffer) == 516); // the first non-full packet is the last.
return $ret;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.socket-create.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43057
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43057&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43057&report=yes
Search -- http://master.php.net/manage/user-notes.php