英文:
Unable to make a UDP request
问题
我正在尝试使用Go语言构建一个BitTorrent客户端。我需要通过UDP请求连接到各个追踪器。为此,我使用了net
包,并进行如下操作:
net.Dial("udp", "udp://hostname:1337/announce")
但是我遇到了一个"地址中有太多冒号"的错误。
如果我尝试这样写:
net.Dial("udp", "hostname:1337/announce")
我会得到一个"未提供节点名或服务名,或者未知"的错误。
我该如何解决这个问题?
英文:
I am trying to build a BitTorrent client in go. I need to make UDP requests to connect to the various trackers. For this I use the net
package and do this:
net.Dial("udp", "udp://hostname:1337/announce")
I get a "too many colons in address" error.
If I try this:
net.Dial("udp", "hostname:1337/announce")
I get a "nodename nor servname provided, or not known" error.
How do I fix this?
答案1
得分: 2
所以你需要将其发送到由.torrent
元文件(announce
字段)提供的IP
地址和port
。
一旦你打开了net.Conn
,你可以使用conn.Write()
向套接字写入数据,类似地,你可以使用conn.Read()
读取数据。
所以你已经接近成功了:
conn, err := net.Dial("udp", announceAddr:Port)
在使用HTTP
连接时,是的,你使用/announce
端点,但不是使用UDP
。
规范解释了要读取和写入多少字节(一开始是固定的,但后来在读取对等列表时是动态的)。我发现这个链接非常有用:https://github.com/naim94a/udpt/wiki/The-BitTorrent-UDP-tracker-protocol
英文:
So you'll need to send it to the IP
address and port
as provided by the .torrent
metafile (announce
field).
And once you open the net.Conn
you can conn.Write()
to the socket and similarly conn.Read()
So you've just about gotten i:
conn, err := net.Dial("udp", announceAddr:Port)
When connecting with HTTP
, yeah you use the /announce
endpoint, but not with UDP
The specs explain how many bytes to read and write (it is fixed at first, but later dynamic when it comes to reading the peer list). I've found this link, rather, the most useful: https://github.com/naim94a/udpt/wiki/The-BitTorrent-UDP-tracker-protocol
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论