Kaynağa Gözat

Create dial.go

glight2000 10 yıl önce
ebeveyn
işleme
c0410a66a8
1 değiştirilmiş dosya ile 24 ekleme ve 0 silme
  1. 24 0
      eBook/examples/chapter_15/dial.go

+ 24 - 0
eBook/examples/chapter_15/dial.go

@@ -0,0 +1,24 @@
+// make a connection with www.example.org:
+package main
+
+import (
+	"fmt"
+	"net"
+	"os"
+)
+
+func main() {
+	conn, err := net.Dial("tcp", "192.0.32.10:80") // tcp ipv4
+	checkConnection(conn, err)
+	conn, err = net.Dial("udp", "192.0.32.10:80") // udp
+	checkConnection(conn, err)
+	conn, err = net.Dial("tcp", "[2620:0:2d0:200::10]:80") // tcp ipv6
+	checkConnection(conn, err)
+}
+func checkConnection(conn net.Conn, err error) {
+	if err != nil {
+		fmt.Printf("error %v connecting!")
+		os.Exit(1)
+	}
+	fmt.Println("Connection is made with %v", conn)
+}