0

I've created a client which sends the get server list command, but the received bytes isn't readable.

Here is my code :

    byte[] receiveBytes = udp.Receive(ref RemoteIpEndPoint);

    string[] returnData = Encoding.Default.GetString(udp.Receive(ref RemoteIpEndPoint)).Split('\\');
    textBox1.Lines = returnData;

in locals i see the write value enter image description here

but program show me this

enter image description here

can someone tell me what is wrong with my code ?

ok i change my code to

 yte[] receiveBytes = udp.Receive(ref RemoteIpEndPoint);
    int size = receiveBytes.Length;
    int i = 0;
    while ( i <= size-5 )
    {

        string ip = receiveBytes[i] + "." + receiveBytes[i + 1] + "." + receiveBytes[i + 2] + "." + receiveBytes[i + 3] ;
        int port = receiveBytes[i + 4] * 256 + receiveBytes[i + 5];

        textBox1.Text += ip + ":" + port.ToString() + Environment.NewLine;
        i = i + 6;
    }

but received data isnt right !

i find smiler code on php and its working .

$data = explode("\\", $data);

for($i=0, $o=0; $i<count($data); $i++) {
    if (strlen($data[$i])>=4) { //fix

        // First 4 bytes are the ip:
        $list_server[$o]['ip']=ord($data[$i][0]).".".ord($data[$i][1]).".".ord($data[$i][2]).".".ord($data[$i][3]);

        // Last 2 bytes are the port, Takes penultimate number and multiply by 256 and sum with the last number:
        $list_server[$o]['port']=(ord($data[$i][4])*256) + ord($data[$i][5]);
        //GetName($list_server[$o]['ip'],$list_server[$o]['port']);
        $o++;
    }
}

i cant guess what is wrong with my code .

madman
  • 319
  • 2
  • 6
  • 19

1 Answers1

0

You are assuming the entire UDP packet is an English readable message. This is almost never the case.

You will need to research the protocol used by the server you are connecting to. For instance it may be sending you a list of identifiers that it expects you to return to find more details for that particular server.

EDIT:

Your latest PHP version uses a \ to separate addresses, so they are sent as follows:

127.0.0.1:8080\192.168.0.1:9000\8.8.8.8:80

Which would be encoded as (spaces added for clarity):

7F000001 1F90 5C c0A80001 2328 5C 08080808 0050

I don't see any reference to the \ character in your example so you likely would decode this example as (note you would normally fail to consume it but you modified your guard clause to hide this bug):

127.0.0.1:8080
92.192.168.0:291
40.92.8.8:2056
Guvante
  • 18,775
  • 1
  • 33
  • 64
  • i figure it out the problem was on string and it should be an int . now im facing another problem ... – madman Mar 18 '13 at 18:33
  • @madman: What output are you getting versus what you are expecting? Your latest C# code seems accurate. – Guvante Mar 19 '13 at 04:59
  • problem is my ip:port isnt accurate and its wrong . but php code give me accurate result i cant find out why – madman Mar 20 '13 at 13:56