Sending and recieving data from udp

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mckooter
Forum Commoner
Posts: 26
Joined: Fri Jul 28, 2006 10:02 pm

Sending and recieving data from udp

Post by mckooter »

Okay, ive been playing with this all night long, well... for two days now, I am trying to send data, that i have in binary format to a udp port on a server and then recieve the response. This is the basic script i have

Code: Select all

<?php
 
$binary_msg = chr(0x00).chr(0x02).chr(0xF1).chr(0x26).chr(0x01).chr(0x26).chr(0xF0).chr(0x90).chr(0xA6).chr(0xF0).chr(0x26).chr(0x57).chr(0x4E).chr(0xAC).chr(0xA0).chr(0xEC).chr(0xF8).chr(0x68).chr(0xE4).chr(0x8D).chr(0x21);
 
$fp = fsockopen("udp://<<ipaddresshere>>",2302,$errno,$errstr);
if (!$fp) {
    echo "ERROR: $errno - $errstr<br />\n";
} else {
    fwrite($fp, $binary_msg);
    echo fread($fp, 26);
    fclose($fp);
}
?>
i have been playing with different ways to read that data, and well, im not even sure that im capturing it right, its basically straight from the php.net site, but i have been researching and it appears that i am somewhat on the right path. I have found that i cannot just print the $fp data, as i just get "Resource id #1" as its not data but just seems to show me how to get the data. I really am lost, i have tried everything i could research and still get nothing. I fear that im much further off than i think.

Thanks for anything that you may have to help
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Sending and recieving data from udp

Post by requinix »

$fp is a resource. You can't print it.

Code: Select all

var_dump(bin2hex(fread($fp, 26)));
What does that give?
mckooter
Forum Commoner
Posts: 26
Joined: Fri Jul 28, 2006 10:02 pm

Re: Sending and recieving data from udp

Post by mckooter »

Warning: fread(): 1 is not a valid stream resource in /homepages/14/d187646903/htdocs/sites/tekagis/port.php5 on line 12
string(0) ""
thats the same error i was getting before (with fread), is it possible the server is simply not sending anything back?


EDIT: that was running it under the php5 processor, if i run it under php4 i get
string(0) ""
makes me think im missing something with what im sending




also, someone may or may not be able to use this, but the data im trying to get is from a gaming server, and after several wireshark sessions and ripping apart a exe i use to ping the server form command line i came to this. here is the relevant info from the exe (this is C# i believe)

Code: Select all

            Socket s = new Socket(lep.Address.AddressFamily,
                SocketType.Dgram,
                ProtocolType.Udp);
 
            byte[] inputToBeSent = new byte[21];
 
            // Create the "special" packet that will return the correct data
            inputToBeSent[0] = 0x00;
            inputToBeSent[1] = 0x02;
            inputToBeSent[2] = 0xF1;
            inputToBeSent[3] = 0x26;
            inputToBeSent[4] = 0x01;
            inputToBeSent[5] = 0x26;
            inputToBeSent[6] = 0xF0;
            inputToBeSent[7] = 0x90;
            inputToBeSent[8] = 0xA6;
            inputToBeSent[9] = 0xF0;
            inputToBeSent[10] = 0x26;
            inputToBeSent[11] = 0x57;
            inputToBeSent[12] = 0x4E;
            inputToBeSent[13] = 0xAC;
            inputToBeSent[14] = 0xA0;
            inputToBeSent[15] = 0xEC;
            inputToBeSent[16] = 0xF8;
            inputToBeSent[17] = 0x68;
            inputToBeSent[18] = 0xE4;
            inputToBeSent[19] = 0x8D;
            inputToBeSent[20] = 0x21;
 
            try
            {
                DateTime dtStart = DateTime.Now;
 
                // Sends datagram to the IpEndPoint specified. This call blocks. 
                s.SendTo(inputToBeSent, 0, inputToBeSent.Length, SocketFlags.None, lep);
 
                // Creates an IpEndPoint to capture the identity of the sending host.
                IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
                EndPoint tempRemoteEP = (EndPoint)sender;
 
                // Creates a byte buffer to receive the message.
                int iAvailable  = s.Available;
                int iSecCount = 0;
Post Reply