SHARE
TWEET

Untitled

a guest Jul 20th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "log"
  7.     "net"
  8.     "time"
  9. )
  10.  
  11. func main() {
  12.     // fmt.Println("hello world")
  13.     timeout := time.Duration(2 * time.Second)
  14.     // _ , err := net.DialTimeout("tcp", "localhost:10800", timeout)
  15.  
  16.     // if err != nil {
  17.     //  log.Println("Sit unreachable, error", err)
  18.     // } else {
  19.     //  log.Printf("ok")
  20.     // }
  21.  
  22.     ports := []int{10800, 10801, 8123, 2200, 31500, 8888}
  23.  
  24.     for _, port := range ports {
  25.         // log.Println(port)
  26.         remote := fmt.Sprintf("localhost:%d", port)
  27.         conn, err := net.DialTimeout("tcp", remote, timeout)
  28.         if err != nil {
  29.             raddr, err := net.ResolveUDPAddr("udp", remote)
  30.             udp_conn, err := net.DialUDP("udp", nil, raddr)
  31.             defer udp_conn.Close()
  32.             if err != nil {
  33.                 log.Printf("udp open error: %v", err)
  34.                 continue
  35.             } else {
  36.                 p := make([]byte, 1)
  37.                 udp_conn.Write([]byte("ping"))
  38.                 _, err := bufio.NewReader(udp_conn).Read(p)
  39.                 if err != nil {
  40.                     log.Println("udp read error: ", err)
  41.                     continue
  42.                 } else {
  43.                     log.Printf("%s udp ok", remote)
  44.                 }
  45.             }
  46.         } else {
  47.             defer conn.Close()
  48.             log.Printf("%s tcp ok", remote)
  49.         }
  50.     }
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top