无法进行UDP请求

huangapple go评论80阅读模式
英文:

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

huangapple
  • 本文由 发表于 2017年6月6日 21:19:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/44391148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定