當前位置: 首頁>>代碼示例>>Golang>>正文


Golang TCPAddr.Network方法代碼示例

本文整理匯總了Golang中net.TCPAddr.Network方法的典型用法代碼示例。如果您正苦於以下問題:Golang TCPAddr.Network方法的具體用法?Golang TCPAddr.Network怎麽用?Golang TCPAddr.Network使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net.TCPAddr的用法示例。


在下文中一共展示了TCPAddr.Network方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Send

// Send DataItems to Zabbix server and wait for response.
// Returns encountered fatal error like I/O and marshalling/unmarshalling.
// Caller should inspect response (and in some situations also Zabbix server log)
// to check if all items are accepted.
func Send(addr *net.TCPAddr, di DataItems) (res *Response, err error) {
	b, err := di.Marshal()
	if err != nil {
		return
	}

	// Zabbix doesn't support persistent connections, so open/close it every time.
	conn, err := net.DialTCP(addr.Network(), nil, addr)
	if err != nil {
		return
	}
	defer conn.Close()

	_, err = conn.Write(b)
	if err != nil {
		return
	}

	buf := make([]byte, 8)
	_, err = io.ReadFull(conn, buf[:5])
	if err != nil {
		return
	}
	if !bytes.Equal(buf[:5], header) {
		err = ErrBadHeader
		return
	}

	_, err = io.ReadFull(conn, buf)
	if err != nil {
		return
	}
	var datalen uint64
	err = binary.Read(bytes.NewBuffer(buf), binary.LittleEndian, &datalen)
	if err != nil {
		err = ErrBadHeader
		return
	}

	buf = make([]byte, datalen)
	_, err = io.ReadFull(conn, buf)
	if err != nil {
		return
	}

	res = new(Response)
	err = json.Unmarshal(buf, res)
	if err == nil {
		m := infoRE.FindStringSubmatch(res.Info)
		if len(m) == 3 {
			p, _ := strconv.Atoi(m[1])
			f, _ := strconv.Atoi(m[2])
			res.Processed = p
			res.Failed = f
		}
	}
	return
}
開發者ID:hynd,項目名稱:zabbix-sender,代碼行數:62,代碼來源:sender.go

示例2: CheckStatusOld

func CheckStatusOld(addr *net.TCPAddr, host string, port uint16) (*MinecraftStatus, time.Duration, error) {
	conn, err := net.DialTCP(addr.Network(), nil, addr)
	if err != nil {
		return nil, 0, fmt.Errorf("CheckStatusOld error dialing: %s", err)
	}
	defer conn.Close()
	status, ping, err := CheckStatusOldConn(conn, host, port)
	return status, ping, err
}
開發者ID:Craftserve,項目名稱:mcstatus,代碼行數:9,代碼來源:old.go

示例3: main

func main() {
	//exit := make(chan bool)
	ip := net.ParseIP("127.0.0.1")
	port := 81
	tcp_addr := net.TCPAddr{ip, port, ""}
	udp_addr := net.UDPAddr{ip, port, ""}
	fmt.Println(tcp_addr)
	fmt.Println(udp_addr)
	fmt.Println(tcp_addr.Network())
	fmt.Println(udp_addr.Network())
}
開發者ID:cch123,項目名稱:test,代碼行數:11,代碼來源:sockettest.go


注:本文中的net.TCPAddr.Network方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。