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


Golang PacketConn.ReadFrom方法代碼示例

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


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

示例1: runPayloadTransponder

// runPayloadTransponder transmits IPv4 datagram payloads to the
// loopback address or interface and captures the loopback'd datagram
// payloads.
func runPayloadTransponder(t *testing.T, c *ipv4.PacketConn, wb []byte, dst net.Addr) {
	cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface
	rb := make([]byte, 1500)
	for i, toggle := range []bool{true, false, true} {
		if err := c.SetControlMessage(cf, toggle); err != nil {
			t.Fatalf("ipv4.PacketConn.SetControlMessage failed: %v", err)
		}
		c.SetTOS(i + 1)
		var ip net.IP
		switch v := dst.(type) {
		case *net.UDPAddr:
			ip = v.IP
		case *net.IPAddr:
			ip = v.IP
		}
		if ip.IsMulticast() {
			c.SetMulticastTTL(i + 1)
		} else {
			c.SetTTL(i + 1)
		}
		c.SetDeadline(time.Now().Add(100 * time.Millisecond))
		if _, err := c.WriteTo(wb, nil, dst); err != nil {
			t.Fatalf("ipv4.PacketConn.WriteTo failed: %v", err)
		}
		_, cm, _, err := c.ReadFrom(rb)
		if err != nil {
			t.Fatalf("ipv4.PacketConn.ReadFrom failed: %v", err)
		}
		t.Logf("rcvd cmsg: %v", cm)
	}
}
開發者ID:alisle,項目名稱:elleLog,代碼行數:34,代碼來源:mocktransponder_test.go

示例2: writeThenReadPayload

// writeThenReadPayload transmits IPv4 datagram payloads to the
// loopback address or interface and captures the loopback'd datagram
// payloads.
func writeThenReadPayload(t *testing.T, i int, c *ipv4.PacketConn, wb []byte, dst net.Addr) []byte {
	rb := make([]byte, 1500)
	c.SetTOS(i + 1)
	var ip net.IP
	switch v := dst.(type) {
	case *net.UDPAddr:
		ip = v.IP
	case *net.IPAddr:
		ip = v.IP
	}
	if ip.IsMulticast() {
		c.SetMulticastTTL(i + 1)
	} else {
		c.SetTTL(i + 1)
	}
	c.SetDeadline(time.Now().Add(100 * time.Millisecond))
	if _, err := c.WriteTo(wb, nil, dst); err != nil {
		t.Fatalf("ipv4.PacketConn.WriteTo failed: %v", err)
	}
	n, cm, _, err := c.ReadFrom(rb)
	if err != nil {
		t.Fatalf("ipv4.PacketConn.ReadFrom failed: %v", err)
	}
	t.Logf("rcvd cmsg: %v", cm)
	return rb[:n]
}
開發者ID:98pm,項目名稱:docker,代碼行數:29,代碼來源:mocktransponder_test.go

示例3: benchmarkReadWriteIPv4UDP

func benchmarkReadWriteIPv4UDP(b *testing.B, p *ipv4.PacketConn, wb, rb []byte, dst net.Addr, ifi *net.Interface) {
	cm := ipv4.ControlMessage{TTL: 1}
	if ifi != nil {
		cm.IfIndex = ifi.Index
	}
	if _, err := p.WriteTo(wb, &cm, dst); err != nil {
		b.Fatalf("ipv4.PacketConn.WriteTo failed: %v", err)
	}
	if _, _, _, err := p.ReadFrom(rb); err != nil {
		b.Fatalf("ipv4.PacketConn.ReadFrom failed: %v", err)
	}
}
開發者ID:cfibmers,項目名稱:diego-release,代碼行數:12,代碼來源:unicast_test.go


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