我正在尝试在objective-c中创建一个torrent scraper,我正在使用CocoaAsyncSocket通过UDP发送数据包。遵循BitTorrent UDP 跟踪器协议。我已经使用 Wireshark 验证了数据包已经发送,但是跟踪器没有发回任何东西。我假设我在将发送的数据放在一起时做错了,因为我对数据操作的经验很少。现在我只是想成功完成协议的连接请求。这是代码
-(void)connect {
NSString *host = @"tracker.publicbt.com";
GCDAsyncUdpSocket *socket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[socket connectToHost:host onPort:80 error:nil];
}
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didConnectToAddress:(NSData *)address {
uint64_t connection_id = htonl(0x41727101980);
uint32_t action = htonl(0);
uint32_t transaction_id = htonl(122);
NSMutableData *data = [NSMutableData data];
[data appendBytes:&connection_id length:sizeof(connection_id)];
[data appendBytes:&action length:sizeof(action)];
[data appendBytes:&transaction_id length:sizeof(transaction_id)];
[sock sendData:data toAddress:address withTimeout:-1 tag:1];
}