UDP Tracker not responding

Posted by kelton52 on Stack Overflow See other posts from Stack Overflow or by kelton52
Published on 2012-07-02T20:30:54Z Indexed on 2012/07/02 21:16 UTC
Read the original article Hit count: 230

Filed under:
|
|

Alright, so I'm trying to connect to UDP trackers using c#, but I never get a response. I also don't get any errors. Here's my code.

namespace UDPTester
{
    class MainClass
    {
        public static bool messageReceived = false;
        public static Random Random = new Random();
        public static void LOG(string format, params object[] args)
        {
            Console.WriteLine (format,args);    
        }
        public static void Main (string[] args)
        {
            LOG ("Creating Packet...");
            byte[] packet;
            using(var stream = new MemoryStream())
            {
                var bc = new MiscUtil.Conversion.BigEndianBitConverter();
                using(var br = new MiscUtil.IO.EndianBinaryWriter(bc,stream))
                {
                    LOG ("Magic Num: {0}",(Int64)0x41727101980);
                    br.Write (0x41727101980);
                    br.Write((Int32)0);
                    br.Write ((Int32)Random.Next());
                    packet = stream.ToArray();
                    LOG ("Packet Size: {0}",packet.Length);
                }
            }
            LOG ("Connecting to tracker...");
            var client = new System.Net.Sockets.UdpClient("tracker.openbittorrent.com",80);
            UdpState s = new UdpState();
            s.e = client.Client.RemoteEndPoint;
            s.u = client;
            StartReceiving(s);

            LOG ("Sending Packet...");
            client.Send(packet,packet.Length);
            while(!messageReceived)
            {
                    Thread.Sleep(1000);
            }
            LOG ("Ended");

        }
        public static void StartReceiving(UdpState state)
        {
            state.u.BeginReceive(ReceiveCallback,state);
        }

        public static void ReceiveCallback(IAsyncResult ar)
        {
            UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
            IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;

            Byte[] receiveBytes = u.EndReceive(ar, ref e);
            string receiveString = Encoding.ASCII.GetString(receiveBytes);

            LOG("Received: {0}", receiveString);
            messageReceived = true;
            StartReceiving((UdpState)ar.AsyncState);
        }

    }
    public class UdpState
    {
        public UdpClient u;
        public EndPoint e;
    }
}

I was using a normal BinaryWriter, but that didn't work, and I read somewhere that it wants it's data in BigEndian.

This doesn't work for any of the UDP trackers I've found, any ideas why I'm not getting a response? Did they maybe change the protocol and not tell anyone? HTTP trackers all work fine.

Trackers I've tried

udp://tracker.publicbt.com:80

udp://tracker.ccc.de:80

udp://tracker.istole.it:80

Also, I'm not interested in using MonoTorrent(and when I was using it, the UDP didn't work anyways).

Protocol Sources

http://xbtt.sourceforge.net/udp_tracker_protocol.html

http://www.rasterbar.com/products/libtorrent/udp_tracker_protocol.html

© Stack Overflow or respective owner

Related posts about c#

Related posts about udp