|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-02-19 22:29 UTC] rip at undernet dot org
I am using PHP for UDP stuff (to block access to users based on a DNSBL).
With TCP connections, PHP detects the end of a packet.
This feature doesn't exist while dealing with UDP but it could exist.
ie:
If I do a TCP connection and I expect 1 packet (but doesn't know it's size) I'll do this :
$fp=fsockopen ("www.stuff.mars", 80, $errno, $errstr, 30);
fputs ($fp, "GET / HTTP/1.0\r\nHost: www.stuff.mars\r\n\r\n");
echo fread ($fp,4096);
fclose ($fp);
Even if the packet isn't 4096 bytes long, it will be returned entirely until fread gets to it's end.
With UDP, the end of the packet CAN be detected but IS NOT detected, if you use fgets you get NOTHING and if you use fread you get only the number of bytes you asked for. Worst is that if you set 4096 as the length for fread, since it can't detect the end of a packet, it waits and stay stuck on it until it received 4096 bytes worst of packets.
ie :
$fp=fsockopen ("udp://boo.stuff.mars", 13, $errno, $errstr, 3);
fputs ($fp, "\n");
echo fread ($fp,4096);
fclose ($fp);
Though I think you could do even better.
ie :
$fp=fsockopen ("udp://ns.stuff.mars", 53, $errno, $errstr, 3);
fputs ($fp, $dns_packet);
while ($rcvd=fread($fp,4096)) echo ord($rcvd);
fclose ($fp);
That's technically doable...
I know that there are new socket functions and I could probably use those to do what I want to do.
But this report is just about if you want to support UDP sockets as one of the standard features of PHP, then it needs to be well done.
Thank you guys, keep up the good work.
[ I give $10000 to the first one who come up with a PHP compiler for *nix and Win. ]
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 15 03:00:01 2025 UTC |
It also says it should only be used in connected sockets. Here is a working exemple from a porky sourcecode I have : loop: len = recvfrom(sock, data, 50, 0, (struct sockaddr *)&sai, &s_sai); printf("%d bytes from %s:%d - %s\n",len,inet_ntoa(sai.sin_addr),ntohs(sai.sin_port),data); goto loop; The default size is 50, if the UDP packet is < 50 (ie:42) it will print : 42 bytes from 192.168.0.1:8000 - This is only a test. This is only a test!! So if you modify the recv call to a recvfrom call, it won't break TCP and will make UDP work a better way.