doujiao0110 2013-10-01 04:12
浏览 78

UDP ping-尝试获取端口无法访问错误

I want to implement UDP ping for my client/server application where the client sends UDP packets to any of the server's ephemeral port in trying to get ICMP port unreachable reply.

I have the following code. ReadFromUDP() return error = nil and 0 bytes read from socket.

Question is, how can I read in specific port unreachable ICMP reply from the server?

conn, _ := net.ListenUDP("udp4", src)
defer conn.Close()

t := time.Now()
conn.SetDeadline(t.Add(100 * time.Millisecond))
conn.SetReadDeadline(t.Add(250 * time.Millisecond))

w, e := conn.WriteTo([]byte("PING"), dst)
if e != nil {
    return nil, errors.New("Failed to send UDP4 ping request")
}

r, _, e := conn.ReadFromUDP(b4)
if e != nil {
    return nil, errors.New("Failed to read UDP4 response packets")
}
  • 写回答

1条回答 默认 最新

  • dongyou2305 2013-10-05 09:11
    关注

    Check the first 2 bytes of the reply message for type 3, code 3 (port unreachable):

    To quote RFC792:

    Destination Unreachable Message
    
        0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |     Type      |     Code      |          Checksum             |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                             unused                            |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |      Internet Header + 64 bits of Original Data Datagram      |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    
       IP Fields:
    
       Destination Address
    
          The source network and address from the original datagram's data.
    
       ICMP Fields:
    
       Type
    
          3
    
       Code
    
          0 = net unreachable;
    
          1 = host unreachable;
    
          2 = protocol unreachable;
    
          3 = port unreachable;
    
          4 = fragmentation needed and DF set;
    
          5 = source route failed.
    
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)