Jump to content


New Members
  • Posts

  • Joined

  • Last visited

GaryM's Achievements


Newbie (1/5)



  1. kicken... that works beautifully. I knew it was simple. I had forgotten about ord() - the anti chr(). Thanks everyone. I still want to know what IDE (not eclipse) do you guys recommend. (I've had a botched eclipse install on this PC and I gave up trying to fix it.)
  2. Nonsense code or not, it works now and I've logged thousands of bytes without error. Removing the unpack() line and adjusting for variable names, I'm back where I was before I showed up here. The nonsense code I have came from PHP.net Jacques1 Show us, please. How would a guru such as yourself, unpack that binary data? You give comments and criticism freely. can you even do it? Again, what IDE are the cool kids here using?
  3. https://forums.phpfreaks.com/topic/303214-array-or-array-or-wha/?p=1542940 I think jinerjm's above post on arrays should be a forum sticky
  4. So a old dad and his son were talking on the porch. Dad says, "Your mother and I have been going to the memory clinic for us old timers. I think its helping." Son: "oh? Whats it called?" Dad: "Um.. well darn. Its a flower. Usually red but they come in other colors... Um... thorny... uh..." Son: A rose? Dad: "YES! That's it! - Hang on" He turns to the door and shouts "Hey Rose - Whats the name of that memory clinic we go to?" ================================= Okay, Jacques1: I did what you said, with the exception of copying jinerjm's example, only to avoid any typos. This is otherwise an original work. jinerjm... The following code works just fine. Thank you guys! while(1) { $r = socket_recvfrom($socket, $buf, 16, 0, $from, $port); // reads at most 16 bytes. I only need 1. //Convert to array of decimal values $array = unpack("C*", $buf); // unpack() smells like split() from my perl days. foreach ($array as $k=>$v) // "$array as $k=>$v"? I'm lost on this one. Anyone care to explain?? { echo "$k = $v \n"; // console output for testing. \n instead of <BR> } } I hope the above snippet can be of use to the next person trying to read UDP datagrams from the web. Also whats a good non-eclipse IDE ? Preferably free. Thank you very much!
  5. I'm using notepad++. Its nice for highlighting things but that doesn't tell me what the function does. (does it? I don't believe it does) I'm sorry I wasn't clear. I dont know what this function is doing. I dont know what the error looks like, and I dont know why the print_r thing skips a zero. I'm not asking for anyone to write my code. I'm certain there is an easy way to get data out of an array. I dont know what array() does, beyond the man page. Its a function but the author is assigning it to a $variable?? How does that even work? I understand print_r is showing us a breakdown of the array's contents. How do I get the first (or second, whatever) out of the array without using print_r ?? I wish to operate on the byte (or char) programmatically, rather than look at it. Whats missing the '$'? Where ? I set up the error checking as ginerjm recommended but it means nothing to me. Because I dont know arrays? Here's the output: Notice: Undefined offset: 0 in /home/pool/public_html/socket_recvfrom.php on line 24 Notice: Undefined offset: 0 in /home/pool/public_html/socket_recvfrom.php on line 27 Array ( [1] => 249 [2] => 0 [3] => 0 [4] => 0 ) Received 0 from remote address nnn.nnn.nnn.nnn and remote port 8888 Because of PHP's "gender fluidity" with variable casting, I'm not sure if the zero being output is a CHAR, INT, One-Byte-String... for ($i = 0; $i < count($array); $i++) { $chr_array[] = chr($array[$i]); // line 24 if($i == '0') { $mychr = chr($array[$i]); // line 27 } } "Undefined offset" makes no sense to me. How do I define it? Again, I do well with most parts of PHP but arrays ...
  6. I dont understand arrays in PHP and its necessary to finally fess up and ask for help. It seems so trivial,, but I've spent Whole DAYS trying to make sense of this. (I came from assemble language programming). I am receiving UDP packets of a known size. in this case, 4 bytes of binary data. It gets unpacked ( I dont know why. I cut and pasted this simple parser from an example). All I want is the array[0] byte. I'm trying to avoid complex loops and pointers. // First lets grab a data packet and put our few bytes into the variable $buf while(1) { socket_recvfrom($socket, $buf, 16, 0, $from, $port); // Easy! but the UDP data in $buf is binary not useful like this. //Convert to array of decimal values $array = unpack("C*", $buf); // 'C' tells unpack to make unsigned chars from the binary. //Convert decimal values to ASCII characters: $chr_array = array(); // wait a second. What is happening here? for ($i = 0; $i < count($array); $i++) { $chr_array[] = chr($array[$i]); // Whats going on here? Anyone? } // So now I can assume the first byte of this $chr_array[] is my prize, right? $mychr = chr_array[0]; // Nope. Looking at $chr_array[0] either reveals a zero or the actual word "Array", depending on how late I've stayed up. print_r($array); // This is my debugger. It shows SOMETHING is working. echo "Received $mychr from remote $address $from and remote port $port" . PHP_EOL; } socket_close($sock); Output of above code... assuming the value read was 60 decimal: Array ( [1] => 60 [2] => 0 [3] => 0 [4] => 0 ) Received 0 from remote address nnn.nnn.nnn.nnn and remote port 8888 So print_r knows what to do, the last line of my code doesn't. Any help would be appreciated, as this goes to the heart of my issue of being mystified by how PHP makes and processes arrays. Its clearly not just a string of numbers. I'm trying to pull ONE printable number from that buffer. Halp!
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.