golang编写ping失败,求大神指点

taoshuye · 2016-05-27 08:03:19 · 2326 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2016-05-27 08:03:19 的主题,其中的信息可能已经有所发展或是发生改变。

环境是在windows7下的LiteIDE集成环境,代码如下:

package main

import (
    "bytes"
    "encoding/binary"
    "fmt"
    "net"
)

type ICMP struct {
    Type        uint8
    Code        uint8
    Checksum    uint16
    Identifier  uint16
    SequenceNum uint16
}

func CheckSum(data []byte) uint16 {
    var (
        sum    uint32
        length int = len(data)
        index  int
    )
    for length > 1 {
        sum += uint32(data[index])<<8 + uint32(data[index+1])
        index += 2
        length -= 2
    }
    if length > 0 {
        sum += uint32(data[index])
    }
    sum += (sum >> 16)

    return uint16(^sum)
}

func main() {
    var (
        icmp  ICMP
        laddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.31.11")}
        raddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.31.1")}
    )
    //如果你要使用网络层的其他协议还可以设置成 ip:ospf、ip:arp 等
    conn, err := net.DialIP("ip4:icmp", &laddr, &raddr)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    defer conn.Close()

    //开始填充数据包
    icmp.Type = 8 //8->echo message  0->reply message
    icmp.Code = 0
    icmp.Checksum = 0
    icmp.Identifier = 0
    icmp.SequenceNum = 0

    var (
        buffer bytes.Buffer
    )
    //先在buffer中写入icmp数据报求去校验和
    binary.Write(&buffer, binary.BigEndian, icmp)
    icmp.Checksum = CheckSum(buffer.Bytes())
    //然后清空buffer并把求完校验和的icmp数据报写入其中准备发送
    buffer.Reset()
    binary.Write(&buffer, binary.BigEndian, icmp)

    if _, err := conn.Write(buffer.Bytes()); err != nil {
        fmt.Println(err.Error())
        return
    }
    fmt.Printf("send icmp packet success!")
}

运行提示:

socket: An attempt was made to access a socket in a way forbidden by its access permissions.

求指点调通~


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2326 次点击  
加入收藏 微博
11 回复  |  直到 2016-05-31 07:16:14
sheepbao
sheepbao · #1 · 9年之前

把conn, err := net.DialIP("ip4:icmp", &laddr, &raddr) 换成 conn, err := net.Dial("ip4:icmp", address) 试试

taoshuye
taoshuye · #2 · 9年之前
sheepbaosheepbao #1 回复

把conn, err := net.DialIP("ip4:icmp", &laddr, &raddr) 换成 conn, err := net.Dial("ip4:icmp", address) 试试

换掉以后,并注解掉//laddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.31.11")} // raddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.31.1")}两行,仍显示: socket: An attempt was made to access a socket in a way forbidden by its access permissions.

sheepbao
sheepbao · #3 · 9年之前
taoshuyetaoshuye #2 回复

#1楼 @sheepbao 换掉以后,并注解掉//laddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.31.11")} // raddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.31.1")}两行,仍显示: socket: An attempt was made to access a socket in a way forbidden by its access permissions.

那就估计是权限的问题了,我在linux下是可以的

sheepbao
sheepbao · #4 · 9年之前
taoshuyetaoshuye #2 回复

#1楼 @sheepbao 换掉以后,并注解掉//laddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.31.11")} // raddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.31.1")}两行,仍显示: socket: An attempt was made to access a socket in a way forbidden by its access permissions.

把防火墙关了,试试

taoshuye
taoshuye · #5 · 9年之前
sheepbaosheepbao #4 回复

#2楼 @taoshuye 把防火墙关了,试试

我把防火墙关了,但还是不行 -_-!!!

taoshuye
taoshuye · #6 · 9年之前
sheepbaosheepbao #4 回复

#2楼 @taoshuye 把防火墙关了,试试

权限是个思路,我再研究研究多谢了

sheepbao
sheepbao · #7 · 9年之前
taoshuyetaoshuye #6 回复

#4楼 @sheepbao 权限是个思路,我再研究研究多谢了

因为明显英文的提示就是说:禁止创建一个socket,我知道在linux下加sudo权限就好了,至于windows,我不用,所以也不懂,帮不到你,不好意思

taoshuye
taoshuye · #8 · 9年之前
sheepbaosheepbao #7 回复

#6楼 @taoshuye 因为明显英文的提示就是说:禁止创建一个socket,我知道在linux下加sudo权限就好了,至于windows,我不用,所以也不懂,帮不到你,不好意思

那还是很感谢

taoshuye
taoshuye · #9 · 9年之前
sheepbaosheepbao #7 回复

#6楼 @taoshuye 因为明显英文的提示就是说:禁止创建一个socket,我知道在linux下加sudo权限就好了,至于windows,我不用,所以也不懂,帮不到你,不好意思

我重新在ubuntu下编写,已经成功,再次感谢!

CodyGuo
CodyGuo · #10 · 9年之前

windows 7以上系统要先以管理员身份打开cmd,然后在执行。

taoshuye
taoshuye · #11 · 9年之前
CodyGuoCodyGuo #10 回复

windows 7以上系统要先以管理员身份打开cmd,然后在执行。

谢啦,还真是的

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传