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)
    }
}
查看全部
dongzhong5756
dongzhong5756
2017/11/30 19:06
  • udp
  • server
  • dns
  • sockets
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复