當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO UDPConn.WriteTo用法及代碼示例

GO語言"net"包中"UDPConn.WriteTo"類型的用法及代碼示例。

用法:

func(c *UDPConn) WriteTo(b []byte, addr Addr)(int, error)

WriteTo 實現 PacketConn WriteTo 方法。

例子:

package main

import (
	"log"
	"net"
)

func main() {
	// Unlike Dial, ListenPacket creates a connection without any
	// association with peers.
	conn, err := net.ListenPacket("udp", ":0")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	dst, err := net.ResolveUDPAddr("udp", "192.0.2.1:2000")
	if err != nil {
		log.Fatal(err)
	}

	// The connection can write data to the desired address.
	_, err = conn.WriteTo([]byte("data"), dst)
	if err != nil {
		log.Fatal(err)
	}
}

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 UDPConn.WriteTo。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。