Kelly Elton
Kelly Elton

Reputation: 4427

UDP Tracker not responding

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

Upvotes: 1

Views: 1711

Answers (1)

Jan Wrobel
Jan Wrobel

Reputation: 7099

UDP is a connectionless protocol, so you won't see any errors if packets are lost or dropped at the destination.

Try following diagnostic steps:

  1. Use packet sniffer (Wireshark is a good one) to check that UDP packets are leaving the machine.
  2. Install some working BitTorrent client, check if it can communicate with the tracker and if yes, use packet sniffer to see how the packets sent by the working client differ from the packets your code generates.

If working client also can not communicate with the tracker, but the UDP traffic is leaving your machine, the UDP packets are likely dropped by a firewall. You can try 'traceroute' tool to diagnose where your packets are dropped (this is not always 100% reliable, because sometimes firewalls drop UDP packets generated by traceroute and do not drop normal UDP traffic).

Upvotes: 1

Related Questions