C#UDP跟踪器协议 - 无法接收数据

时间:2013-04-08 15:12:15

标签: c# udp protocols tracker

你能告诉我为什么我无法接收数据。
我使用了来自http://www.bittorrent.org/beps/bep_0015.html

的UDP跟踪协议

当我尝试从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();

1 个答案:

答案 0 :(得分:2)

好的,经过几个小时的研究后,我终于决定使用Wireshark来查找请求包。

我发现我的数据包的顺序不正确。 ;(

这是新代码。

        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);

现在正在运作!