简体   繁体   中英

C# UDP Tracker Protocol - Cannot receive data

Can you guys tell me why I can't receive data.
I used the UDP Tracker Protocol from : http://www.bittorrent.org/beps/bep_0015.html

Here is my code when I try to get information from udp://tracker.openbittorrent.com:80/announce

        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        client.Connect("tracker.openbittorrent.com", 80);

        byte[] conPacket = new byte[16];
        byte[] temp = BitConverter.GetBytes(0x41727101980);
        byte[] temp2 = BitConverter.GetBytes(0);
        byte[] temp3 = BitConverter.GetBytes(new Random().Next(0, 65535));
        Array.Copy(temp, 0, conPacket, 0, 8);
        Array.Copy(temp2, 0, conPacket, 8, 4);
        Array.Copy(temp3, 0, conPacket, 12, 4);
        client.Send(conPacket);

        byte[] recvPacket = new byte[16];
        client.Receive(recvPacket); <--------------- Waiting for response
        client.Close();

Okay, after few hours of research I finally decided to use Wireshark in order to find the request packet.

I found out that my packet was not in the correct order. ;(

Here is the new code.

        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        client.Connect(tracker, port);

        byte[] conPacket = new byte[16];
        byte[] temp = BitConverter.GetBytes(0x41727101980).Reverse().ToArray();
        byte[] temp2 = BitConverter.GetBytes(0);
        byte[] temp3 = BitConverter.GetBytes(new Random().Next(0, 65535));
        Array.Copy(temp, 0, conPacket, 0, 8);
        Array.Copy(temp2, 0, conPacket, 8, 4);
        Array.Copy(temp3, 0, conPacket, 12, 4);
        //Connect to the protocol
        client.Send(conPacket);

        byte[] response = new byte[16];
        client.Receive(response);

It's working now !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM