dongzhong5756 2017-11-30 19:06
浏览 17
已采纳

UDP套接字未从Go中的服务器读取

I'm developing a fast dns client in go just to mess around with But I'm facing troubles at the time of reading from server responses cause it never arrives and I know it actually did because I have WireShark open and it read the packet.

Here is the code sample(8.8.8.8 is Google DNS and the hex msg is a valid DNS query):

package main

import (
    "fmt"
    "net"
    "encoding/hex"
    "bufio"
)

func CheckError(err error) {
    if err  != nil {
        fmt.Println("Error: " , err)
    }
}

func main() {

    Conn, err := net.Dial("udp", "8.8.8.8:53")
    CheckError(err)

    defer Conn.Close()
    msg, _ := hex.DecodeString("5ab9010000010000000000001072312d2d2d736e2d68357137646e65650b676f6f676c65766964656f03636f6d0000010001")
    scanner := bufio.NewScanner(Conn)
    buf := []byte(msg)
    _, err1 := Conn.Write(buf)
    if err1 != nil {
        fmt.Println(msg, err1)
    }
    for scanner.Scan() {
        fmt.Println(scanner.Bytes())
    }
}

Here you have the proof that it actually arrives: WireShark Screen Capture

I've testes reading directly from conn with:

func main() {

    Conn, err := net.Dial("udp", "8.8.8.8:53")
    CheckError(err)

    defer Conn.Close()
    msg, _ := hex.DecodeString("5ab9010000010000000000001072312d2d2d736e2d68357137646e65650b676f6f676c65766964656f03636f6d0000010001")
    buf := []byte(msg)
    _, err1 := Conn.Write(buf)
    if err1 != nil {
        fmt.Println(msg, err1)
    }
    Reader(Conn)
}

func Reader(conn net.Conn) {
    var buf []byte
    for {
        conn.Read(buf)
        fmt.Println(buf)
    }
}
  • 写回答

1条回答 默认 最新

  • douzi2333 2017-11-30 20:43
    关注

    You can't use bufio around a UDP connection. UDP is not a stream oriented protocol, so you need to differentiate the individual datagrams yourself, and avoid partial reads to prevent data loss.

    In order to read from an io.Reader, you must have space allocated to read into, and you need to use the bytes read value returned from the Read operation. Your example could be reduced to:

    conn, err := net.Dial("udp", "8.8.8.8:53")
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()
    
    msg, _ := base64.RawStdEncoding.DecodeString("WrkBAAABAAAAAAAAEHIxLS0tc24taDVxN2RuZWULZ29vZ2xldmlkZW8DY29tAAABAAE")
    resp := make([]byte, 512)
    
    conn.Write(msg)
    
    n, err := conn.Read(resp)
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf("%q
    ", resp[:n])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果