简体   繁体   English

如何从跟踪器响应中获取对等方的IP和端口

[英]How to get peers' IPs and ports from tracker response

I write simple torrent-client in Qt and I don't understand how to get peers' IPs and ports from tracker response. 我在Qt中编写了简单的torrent客户端,但我不明白如何从跟踪器响应中获取对等方的IP和端口。 I get response successfully, but exactly value of key peers looks unreadable: 我成功获得响应,但是关键peers确切值看起来不可读:

d8:completei1976e10:incompletei54e8:intervali1800e5:peers6:TQ+ГХ§e

Why it looks so and how to make this data readable? 为什么看起来如此以及如何使此数据可读?

In BitTorrent specification it is said that value of peers is always sent in Big-Endian. 在BitTorrent规范中,据说对peers值始终以Big-Endian发送。 I don't know if it can be a reason for unreadability but I suspect that. 我不知道这是否可能是导致无法阅读的原因,但我对此表示怀疑。

Like Encombe said in the comments it's BigEndian. 就像Encombe在评论中所说的是BigEndian。 You can do it programmatically this way: 您可以通过以下方式以编程方式进行操作:

QByteArray peerTmp = "TQ+ГХ§e";
QHostAddress host;
uchar *data = (uchar *)peerTmp.constData();
uint ipAddress = 0;
uint port = (int(data[4]) << 8) + data[5]; 
ipAddress += uint(data[0]) << 24;
ipAddress += uint(data[1]) << 16;
ipAddress += uint(data[2]) << 8;
ipAddress += uint(data[3]);
host.setAddress(ipAddress);
qDebug() << "IP" << host.toString() << ":" << port;

IP 84.81.XX.208:37840 IP 84.81.XX.208:37840

or if you use qFromBigEndian ie 或者如果您使用qFromBigEndian

QHostAddress peerIPAddress(qFromBigEndian<qint32>("TQ+Г"));
qDebug() << "IP" << peerIPAddress.toString();

See : http://doc.qt.io/qt-5/qtnetwork-torrent-trackerclient-cpp.html 请参阅: http : //doc.qt.io/qt-5/qtnetwork-torrent-trackerclient-cpp.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM