你可以使用 ICMP 协议来针对特定的端口吗?

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

Can you target a specific port using icmp?

问题

我相信标题已经很明确了,但以防万一,我会进一步解释。

我正在使用Go编写一个函数,该函数使用icmp来检查特定服务是否正在运行。我从Go中实现的ping命令得到了这个想法。当我尝试使用命令行ping时,它无法解析127.0.0.1:8080,而函数也遵循相同的规则,这是有道理的。但是我是否可以使用icmp来检查该地址和端口,还是应该使用类似tcp的东西来定位一个端口?

现在我有这个简单的函数,我可以使用tcp,但我想知道是否可以使用icmp之类的东西。

func (c *Controller) Ping() error {
    conn, connErr := net.Dial("ip4:icmp", c.APIServerIP)
    if connErr != nil {
        return connErr
    }
    conn.SetDeadline(time.Now().Add(3 * time.Second))
    defer conn.Close()
    return nil
}
英文:

I believe the title is pretty self explanatory, but just in case, I'll explain further.

I am writing a function in Go that uses icmp to check if a particular service is running. I got this idea from ping implemented in Go. When I try command line ping, it can not resolve 127.0.0.1:8080, and the function follows suit, which makes sense. But can I use icmp to check that address and port with something I missing? Or should I just stick something like tcp to target a port?

Right now I have this simple function, and I could just use tcp, but I'm curious if I could use something like icmp.

func (c *Controller) Ping() error {
    conn, connErr := net.Dial("ip4:icmp", c.APIServerIP)
    if connErr != nil {
	    return connErr
    }
    conn.SetDeadline(time.Now().Add(3 * time.Second))
    defer conn.Close()
    return nil
}

答案1

得分: 4

不。ICMP中没有端口。

如果你想知道特定的TCP服务器是否正在运行,尝试连接到它。

英文:

No. There are no ports in ICMP.

If you want to know whether a specific TCP server is running, try to connect to it.

huangapple
  • 本文由 发表于 2016年2月3日 09:24:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/35166895.html
匿名

发表评论

匿名网友

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

确定