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

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)
    }
}

我正在开发一个快速的dns客户端,只是为了搞乱而已,但那时我遇到了麻烦 从服务器响应读取的信息导致它永远不会到达,并且我确实知道是因为打开了WireShark并读取了数据包。 p>

这里是代码示例(8.8.8.8是Google DNS, hex msg是有效的DNS查询): p>

 包main 
 
import(
“ fmt” 
“ net” 
“ encoding / hex” 
  “ bufio” 
)
 
func CheckError(错误错误){
,如果错误!= nil {
 fmt.Println(“ Error:”,err)
} 
} 
 
func main()  {
 
 Conn,err:= net.Dial(“ udp”,“ 8.8.8.8:53")
 CheckError(err)
 
延迟Conn.Close()
 msg,_:=十六进制 .DecodeString(“ 5ab9010000010000000000001072312d2d2d736e2d68357137646e65650b676f6f676c65766964656f03636f6d0000010001”)
扫描器:= bufio.NewScanner(Conn)
 buf:= [] byte(msg)
 _,err1:= ConnW。  
 fmt.Println(msg,err1)
} 
  Scanner.Scan(){
 fmt.Println(scanner.Bytes())
} 
} 
  code>  pre> 
 
 

在这里,您可以证明它确实到达了 : WireShark屏幕捕获 p>

I 've testes通过以下方式直接从conn读取: p>

  func main(){
 
 Conn,err:= net.Dial(“ udp”,“ 8.8.8.8:  53“)
 CheckError(err)
 
延迟Conn.Close()
 msg,_:= hex.DecodeString(” 5ab9010000010000000000001072312d2d2d736e2d68357137646e65650b676f6f676c65766964656f03636f6d0000010001“)
 buf:= [] byte(ms)  := 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)
} 
} 
  code>  pre> 
  div>

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])