Skip to content

Instantly share code, notes, and snippets.

@platinummonkey
Created November 26, 2019 22:14
Show Gist options
  • Select an option

  • Save platinummonkey/28869eff96d094e6d9fdbdecc598d5d2 to your computer and use it in GitHub Desktop.

Select an option

Save platinummonkey/28869eff96d094e6d9fdbdecc598d5d2 to your computer and use it in GitHub Desktop.
Golang Test Dial DNS
package main
// GOOS=linux GOARCH=amd64 go build -o testdial test_dial.go
import (
"flag"
"fmt"
"net"
"time"
)
var options = struct {
network string
host string
port int
}{
network: "tcp",
}
func init() {
flag.StringVar(&options.network, "network", options.network, "network type (tcp,udp)")
flag.StringVar(&options.host, "host", "", "host name (required)")
flag.IntVar(&options.port, "port", 0, "port (required)")
}
func main() {
flag.Parse()
if options.host == "" || options.port <= 0 {
fmt.Println("both host and port must be specified")
return
}
if options.network != "tcp" && options.network != "udp" {
fmt.Println("both network type must be in [udp,tcp]")
return
}
dialer := &net.Dialer{
Timeout: time.Second*15,
}
address := fmt.Sprintf("%s:%d", options.host, options.port)
fmt.Printf("attempting to connect to (%s) %s\n", options.network, address)
conn, err := dialer.Dial(options.network, address)
if err != nil {
fmt.Printf("failed to connect: %v", err)
return
}
conn.Close()
fmt.Println("successfully connected!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment