2012-12-03 66 views
4

我绑定实施一个洪流客户端。我从这里得到我的信息:http://wiki.theory.org/BitTorrent_Tracker_Protocol如何发送请求到洪流跟踪服务器

我想发送一个获取请求到跟踪器来获取对等列表。我应该建立一个udp连接并在端口80连接到udp://tracker.thepiratebay.org吗?这是我的请求消息应该看起来像什么?

"udp://tracker.thepiratebay.org??info_hash=123456789
&peer_id=ABCDEFGHIJKLMNOPQRST 
port=6888 
&downloaded=0 
&left=0 
&event=started " 

这是我的我的代码部分:

char *requestToSend; 
    int sock; 
    struct sockaddr_in servAddr; 
    struct sockaddr_in fromAddr;  
    int fromSize; 
    int respStringLen; 

    int portNum =80; 
    char data_recv[ECHOMAX]; 

    char *hash="123456789"; 
    char *id="ABCDEFGHIJKLMNOPQRST"; 
    char *temp ="udp://tracker.thepiratebay.org??info_hash=123456789\n&peer_id=ABCDEFGHIJKLMNOPQRST\nport=6888\n&downloaded=0\n&left=0\n&event=started"; 
    requestToSend = malloc(sizeof(temp)+1); 
    sprintf(requestToSend, "%s??info_hash=%s\n&peer_id=%s\nport=%s\n&downloaded=0\n&left=0\n&event=started\0","udp://tracker.thepiratebay.org", hash,id,"6888"); 
    printf("%s to send \n", requestToSend); 

    /* Create a datagram/UDP socket */ 
    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ 
     printf("fail create socket"); 
     return 0; 
    } 

    /* Construct the server address structure */ 
    struct hostent *hp = gethostbyname("udp://tracker.thepiratebay.org"); 
    memset(&servAddr, 0, sizeof(servAddr)); /* Zero out structure */ 
    servAddr.sin_family = AF_INET;     /* Internet addr family */ 
    memcpy((char *) &servAddr.sin_addr.s_addr, hp->h_addr, hp->h_length); 
    servAddr.sin_port = htons(portNum);  /* Server port */ 


    //send request to tracker server 
    if (send(sock, requestToSend, strlen(requestToSend), 0) != strlen(requestToSend)){ 
     printf("fail send"); 
     return 0; 
    } 

    /* Recv a response */ 
    fromSize = sizeof(fromAddr); 
    if ((respStringLen = recvfrom(sock, data_recv, ECHOMAX, 0, 
     (struct sockaddr *) &fromAddr, &fromSize)) != strlen(requestToSend)){ 
     printf("fail to recv"); 
     return 0; 
    } 

这是我从一个torrent文件

dict { 
    announce => str = http://tracker.thepiratebay.org/announce (len = 40) 
    announce-list => list [ 
     list [ 
      str = http://tracker.thepiratebay.org/announce (len = 40) 
     ] 
     list [ 
      str = udp://tracker.thepiratebay.org:80/announce (len = 42) 
     ] 
     list [ 
      str = http://tracker.openbittorrent.com/announce (len = 42) 
     ] 
     list [ 
      str = udp://tracker.openbittorrent.com:80/announce (len = 44) 
     ] 
    ] 
    creation date => int = 1174816388 
    encoding => str = UTF-8 (len = 5) 
    info => dict { 
     filehash => str = 
¸¥£öüËXþÐS®(äfn6 (len = 20) 
     length => int = 2222949 
     name => str = Audacity.zip (len = 12) 
     name.utf-8 => str = Audacity.zip (len = 12) 
     piece length => int = 32768 
     pieces => str = (null) (len = 0) 
    } 
} 
d8:announce40:http://tracker.thepiratebay.org/announce13:announce-listll40:http:       //tracker.thepiratebay.org/announceel42:udp://tracker.thepiratebay.org:80/announ       ceel42:http://tracker.openbittorrent.com/announceel44:udp://tracker.openbittorre       nt.com:80/announceee13:creation datei1174816388e8:encoding5:UTF-84:infod8:fileha       sh20: 
¸¥£öüËXþÐS®(äfn66:lengthi2222949e4:name12:Audacity.zip10:name.utf-812:Audacity.z       yf3-ûBÎNrl lengthi32768e6:pieces1360:þ]úÙÉÅ'NÕæ+gd3fi6è6¶ 
+0

尝试重新措辞你的问题。就目前而言,你的目标不是很清楚。你应该特别修改标题。 – Nocturno

+0

您检查了gethostbyname是否返回有效地址?我认为你不应该为其功能添加http://或udp:// protocoll字符串部分。 – Phong

回答

1

你检查的gethostbyname做回一个有效的地址得到些什么? 我认为你不应该添加http://或udp://协议字符串部分作为gethostbyname参数。

struct hostent *hp = gethostbyname("tracker.thepiratebay.org"); 
if(!hp) 
{ 
    herror("gethostbyname(): "); 
    exit(1); 
} 

添加此行以确保gethostbyname确实正常工作。

1

该网站给出的示例不适用于UDP通信。它用于通过TCP进行通信,更具体地说,是HTTP。

0

如果跟踪使用UDP,你不应该发送GET请求,而是建立在指定的端口的连接,然后执行4个步骤:

  1. 发送连接请求
  2. 确认连接请求通过读取16字节响应,并挂在连接标识
  3. 发送连接标识
  4. 解析宣布响应,它的大小取决于指定

在所有的UDP连接需要较少的数据包交换的信息相同数量的多少同行,但它更多地参与实施为客户而言:)

wiki详细说明了UDP消息应该包含什么。他们像您发送的邮件同行(也就是他们使用大端整数编码成字节的固定长度)

specs可以帮助