Search code examples
c#udpbittorrent

Bittorrent Connection ID mismatch


I'm trying to send a connection message to and UDP tracker. When I send the following connection message. I get a message back containing the error: Connection ID mismatch.

I'm using this specification.

        // CONNECTION MESSAGE
        int action = 0;
        long connectionID = 0x41727101980;          
        int sessionId = new Random().Next();

        List<byte> message = new List<byte>();
        message.AddRange(BitConverter.GetBytes(connectionID));
        message.AddRange(BitConverter.GetBytes(action));
        message.AddRange(BitConverter.GetBytes(sessionId));

        byte[] messageData = message.ToArray();
        if (BitConverter.IsLittleEndian)
            Array.Reverse(messageData);


        int n = 0;
        while(!response)
        {
            Thread.Sleep(15 * 2 * n++ * 1000);
            Send(messageData);
        }

Solution

  • I found the problem today, I first have to put the last element in the message so when the array gets reversed to covert to Big Endian it will be the first element.

    message.AddRange(BitConverter.GetBytes(sessionId)); //last element
    message.AddRange(BitConverter.GetBytes(action)); 
    message.AddRange(BitConverter.GetBytes(connectionID)); //first element