Connect.go 492 Bytes
Newer Older
1
package net
AstroSnail's avatar
AstroSnail committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

import (
	"fmt"
	"net"
)

func Connect (
	target string,
) (
	con Conn,
	err error,
) {
	for i := 0; i < tries; i++ {
		if i != 0 {
			fmt.Println("Retrying...")
		}

		c, e := net.DialTimeout("tcp", target, timeout)
		if e != nil {
			err = e
			fmt.Println("Connection failed:", err.Error())
		} else {
			// Success!
			con = Conn{c}
			return
		}
	}

	// Total failure
	fmt.Println("Failed to connect to", target)

	// err still contains last error, so just return
	return
}