sockets - Golang 将 gopacket udpLayer 转换为字节并发送

标签 sockets go layer gopacket

我正在使用 gopacket/layers api 从数据包中提取更新数据,然后通过另一个 udp 流再次发送它,我不确定这样做是否正确,如果有人可以的话,我也会遇到一些错误给我指出正确的方向,那太好了 我的代码

conn, err := net.Dial("udp", 1.1.1.1)

udp, _ := updpLayer.(*layers.UDP)

/*now if i send it like this*/

conn.Write(udp)
/*i get the errors: cannot use udp (type *layers.UDP) as type []byte in argument to conn.Write*/

/*I tried to convert to bytes using unsafe*/

con := *(*[unsafe.Sizeof(udp)]byte)(unsafe.Pointer(&udp))

/* I get cannot use conv (type [8]byte) as type []byte in argument to conn.Write */

/* I used ecoding/gob, but is it sending it as a byte stream?*/

encoder := gob.NewEncoder(conn)
encoder.Encode(udp) //or Encode(udp)

最佳答案

updpLayer 不直接表示数据,因此尝试将其转换为字节是没有意义的。阅读the Contents or Payload field相反。

关于sockets - Golang 将 gopacket udpLayer 转换为字节并发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46960943/

相关文章:

Goroutines 和 mutex

java - Telegram 最新层

c++ - 通过本地代理将 GET 发送到 google。 C++。 Linux。传输控制协议(protocol)

java - 在构建聊天应用程序时,Web Service 是否比 JMS、RMI 或 Socket 编程更好用?

c - 获取客户端IP地址和端口?

go - 这是 goroutine 开销的一个更好的代码

go - 模块声明为 X 但要求为 Y

PHP 将 psd 转换为 jpg,选择图像层

c# - 在 C# 中绘制到新的 "layer"

c - c语言udp套接字编程问题