本文整理汇总了Golang中net.ResolveUDPAddr函数的典型用法代码示例。如果您正苦于以下问题:Golang ResolveUDPAddr函数的具体用法?Golang ResolveUDPAddr怎么用?Golang ResolveUDPAddr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ResolveUDPAddr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sendHistory
func sendHistory(target string) {
ServerAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:"+target)
CheckError(err)
LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
CheckError(err)
Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
CheckError(err)
defer Conn.Close()
msg := "history "
for k, v := range hist {
msg += k + ":" + strconv.FormatInt(v, 10) + " "
}
// fmt.Println("Sending history:", msg)
buf := []byte(msg)
_, err = Conn.Write(buf)
// fmt.Printf("Sent history to %s\n", target)
if err != nil {
fmt.Println(msg, err)
}
}
示例2: main
func main() {
udpAddr, err := net.ResolveUDPAddr("udp", ":20014")
if err != nil {
log.Fatal(err)
}
udpListen, err := net.ListenUDP("udp", udpAddr)
if err != nil {
log.Fatal(err)
}
backupvalue := backup(udpListen)
fmt.Println(backupvalue)
udpListen.Close()
udpAddr, err = net.ResolveUDPAddr("udp", "129.241.187.255:20014")
if err != nil {
log.Fatal(err)
}
udpBroadcast, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
log.Fatal(err)
}
primary(backupvalue, udpBroadcast)
udpBroadcast.Close()
}
示例3: InitSendService
func InitSendService(speed int64, from string, to string) (service *SendService, err error) {
service = new(SendService)
service.speed = speed
addr, err := net.ResolveUDPAddr("udp4", from)
if err != nil {
return nil, err
}
service.addr, err = net.ResolveUDPAddr("udp4", to)
if err != nil {
return nil, err
}
service.conn, err = net.ListenUDP("udp4", addr)
if err != nil {
return nil, err
}
// conn, err := net.Dial("udp", "127.0.0.1:12345")
// if err != nil {
// panic(err)
// }
// service.conn = conn
return service, nil
}
示例4: main
func main() {
//测试手动注册两个通道
sip.GetChannel("10.2.42.197", "10.2.42.133")
sip.GetChannel("10.2.42.133", "10.2.42.197")
//链接WebRTC 服务
go webrtcSocket.Init()
//开启出测地址
go sip.InitListener()
//监听视频端口
service := ":8088"
udpAddr, err := net.ResolveUDPAddr("udp", service)
checkError(err)
conn, err := net.ListenUDP("udp", udpAddr)
checkError(err)
fmt.Println("监听端口")
for {
handleVideo(conn)
}
//监听语音端口
go func() {
serviceAudio := ":11113"
udpAddrAudio, err := net.ResolveUDPAddr("udp", serviceAudio)
checkError(err)
connAudio, err := net.ListenUDP("udp", udpAddrAudio)
checkError(err)
for {
handleAudio(connAudio)
}
}()
}
示例5: main
func main() {
addr, err := net.ResolveUDPAddr("0.0.0.0:10000")
if err != nil {
handle_err(err)
}
addr_out, err := net.ResolveUDPAddr("localhost:10020")
if err != nil {
handle_err(err)
}
c, err := net.ListenUDP("udp4", addr)
if err != nil {
handle_err(err)
}
c_out, err := net.DialUDP("udp4", nil, addr_out)
if err != nil {
handle_err(err)
}
buf := make([]byte, 2000)
for {
length, _, err := c.ReadFrom(buf)
if err != nil {
handle_err(err)
}
c_out.Write(buf[0:length])
}
}
示例6: send
func send(target, msg, tempo string) {
ServerAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:"+target)
CheckError(err)
LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
CheckError(err)
Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
CheckError(err)
// fmt.Println(port)
defer Conn.Close()
msg += ":" + tempo
buf := []byte(msg)
_, err = Conn.Write(buf)
// fmt.Printf("Sent message %s to %s\n", msg, target)
if err != nil {
fmt.Println(msg, err)
}
}
示例7: DoUdpRequest
// Accepts a UdpAction and a one-way channel to write the results to.
func DoUdpRequest(udpAction UdpAction, resultsChannel chan HttpReqResult, sessionMap map[string]string) {
address := SubstParams(sessionMap, udpAction.Address)
payload := SubstParams(sessionMap, udpAction.Payload)
if udpconn == nil {
ServerAddr, err := net.ResolveUDPAddr("udp", address) //"127.0.0.1:10001")
if err != nil {
fmt.Println("Error ResolveUDPAddr remote: " + err.Error())
}
LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
if err != nil {
fmt.Println("Error ResolveUDPAddr local: " + err.Error())
}
udpconn, err = net.DialUDP("udp", LocalAddr, ServerAddr)
if err != nil {
fmt.Println("Error Dial: " + err.Error())
}
}
//defer Conn.Close()
start := time.Now()
if udpconn != nil {
_, err = fmt.Fprintf(udpconn, payload+"\r\n")
}
if err != nil {
fmt.Printf("UDP request failed with error: %s\n", err)
udpconn = nil
}
elapsed := time.Since(start)
resultsChannel <- buildUdpResult(0, 200, elapsed.Nanoseconds(), udpAction.Title)
}
示例8: main
func main() {
ServerAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:10001")
CheckError(err)
LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
CheckError(err)
Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
Conn.SetReadBuffer(1024 * 1024 * 1024)
Conn.SetWriteBuffer(1024 * 1024 * 1024)
CheckError(err)
defer Conn.Close()
start := time.Now()
for i := 0; i < maxTestCount; i++ {
msg := strconv.Itoa(i)
//i++
buf := []byte(msg)
_, err := Conn.Write(buf)
if err != nil {
fmt.Println(msg, err)
}
time.Sleep(time.Nanosecond * 100)
}
timeDiff := time.Since(start)
timeDiffInSec := float64(timeDiff) * 1e-9
fmt.Printf("Excecution time:%f sec, %f ops/s\n", timeDiffInSec, maxTestCount/timeDiffInSec)
}
示例9: main
func main() {
udpAddr, err := net.ResolveUDPAddr("udp", port)
checkError(err)
Listener, err := net.ListenUDP("udp", udpAddr)
checkError(err)
if kind == "server" {
svr = server.NewServer()
go svr.Start(Listener)
sigmask()
svr.Stop()
clean()
}
else if kind == "client" {
cli := client.NewClient(Listener)
serverUdp , err := net.ResolveUDPAddr("udp", serverAddr)
checkError(err)
go cli.Start(serverUdp)
sigmask()
cli.Stop()
clean()
}
else {
log.Println("cli parameter wrong")
os.Exit(1)
}
}
示例10: read
func (this *SocksProxy) read(pack *RequestPack) {
log.Println(pack.DSTAddr, pack.DSTPort)
//获取一个可用的端口
packConn, err := net.ListenPacket("udp", "127.0.0.1:0")
localPortStr := strings.Split(packConn.LocalAddr().String(), ":")[1]
log.Println(localPortStr)
remotAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:"+localPortStr)
locaAddr, _ := net.ResolveUDPAddr("udp", ":"+strconv.Itoa(pack.DSTPort))
conn, err := net.ListenUDP("udp", locaAddr)
if err != nil {
log.Println("连接客户端失败", err.Error())
}
portBuf := bytes.NewBuffer([]byte{})
localPortInt, _ := strconv.Atoi(localPortStr)
binary.Write(portBuf, binary.LittleEndian, int16(localPortInt))
port := portBuf.Bytes()
_, err = conn.WriteToUDP([]byte{5, 0, 0, 1, 127, 0, 0, 1, port[0], port[1]}, remotAddr)
buf := make([]byte, 1024)
n, _, err := conn.ReadFromUDP(buf)
if err != nil {
log.Println("haha", err.Error())
}
log.Println("udp----client---", buf[:n])
}
示例11: newMiaxMcastServer
func newMiaxMcastServer(c Config, src *miaxMessageSource, i int) (mms *miaxMcastServer) {
laddr, err := net.ResolveUDPAddr("udp", c.LocalAddr)
errs.CheckE(err)
mcaddr, err := net.ResolveUDPAddr("udp", c.FeedAddr)
errs.CheckE(err)
laddr.Port += i + LocalPortShift
mcaddr.Port += i
mcaddr.IP[net.IPv6len-1] += (byte)(i)
gapP := c.GapPeriod
if 0 == gapP {
gapP = 0xFFFFFFFFFFFFFFFF
}
mms = &miaxMcastServer{
laddr: laddr,
mcaddr: mcaddr,
src: src,
cancel: make(chan struct{}),
num: i,
gap: 0 != c.GapSize,
gapSize: c.GapSize,
gapPeriod: gapP,
gapCnt: 0,
}
return
}
示例12: main
func main() {
serverAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:8080")
CheckError(err)
localAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:8079")
CheckError(err)
conn, err := net.DialUDP("udp", localAddr, serverAddr)
CheckError(err)
defer conn.Close()
i := 0
fmt.Println("Client connected to: ", serverAddr)
for {
t := test.Packet_Type(1)
h := true
msg := &test.Packet{Type: &t, Heartbeat: &test.Heartbeat{Heartbeat: &h}}
i++
buff, _ := proto.Marshal(msg)
_, err := conn.Write(buff)
CheckError(err)
time.Sleep(time.Second * 1)
}
fmt.Println("Connection closed...")
}
示例13: isLocalAddress
func isLocalAddress(local, localRemote string) bool {
// Resolve the IP returned by the STUN server first.
localRemoteAddr, err := net.ResolveUDPAddr("udp", localRemote)
if err != nil {
return false
}
// Try comparing with the local address on the socket first, but only if
// it's actually specified.
addr, err := net.ResolveUDPAddr("udp", local)
if err == nil && addr.IP != nil && !addr.IP.IsUnspecified() {
return addr.IP.Equal(localRemoteAddr.IP)
}
// Fallback to checking IPs of all interfaces
addrs, err := net.InterfaceAddrs()
if err != nil {
return false
}
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
continue
}
if ip.Equal(localRemoteAddr.IP) {
return true
}
}
return false
}
示例14: setup
func setup() error {
// Resolve bind address
saddr, err := net.ResolveUDPAddr("udp", env.GetDefault("COLLECTD_PROXY_BIND", "127.0.0.1:25827"))
if err != nil {
return err
}
// Listen udp socket
proxy, err = net.ListenUDP("udp", saddr)
if err != nil {
return err
}
// Resolve target address
taddr, err := net.ResolveUDPAddr("udp", env.GetDefault("COLLECTD_PROXY_TARGET", "127.0.0.1:25826"))
if err != nil {
return err
}
target, err = net.DialUDP("udp", nil, taddr)
if err != nil {
return err
}
return nil
}
示例15: main
func main() {
argcnt := len(os.Args)
if argcnt != 2 {
fmt.Println("usage: udptest.exe [IPv6_of_LobaroBox:Port]\r\nExample: udptest.exe [fe80:0000:0000:0000:0211:7d00:0030:8e3f]:5684")
return
}
//Remote Addr
BoxAddr, err := net.ResolveUDPAddr("udp6", os.Args[1])
if err != nil {
log.Fatal(err)
return
}
LocalAddr, err := net.ResolveUDPAddr("udp6", ":0") //:0 => OS sets local port
if err != nil {
log.Fatal(err)
return
}
c, err := net.ListenUDP("udp6", LocalAddr)
if err != nil {
log.Fatal(err)
return
}
defer c.Close()
fmt.Println("Start Listening to:", c.LocalAddr())
//readsocket loop
go func(c *net.UDPConn) {
for {
rxMsg := make([]byte, 512)
n, remote, err := c.ReadFromUDP(rxMsg)
if err != nil {
log.Fatal(err)
return
} else {
fmt.Println("Got from ", remote, " ", n, "Bytes: ", string(rxMsg[:n]))
}
}
}(c)
//write every second current time string to remote "LOBARO IPV6 UNIVERSAL BOX"
for {
b := []byte(time.Now().String())
n, err := c.WriteTo(b, BoxAddr)
if err != nil {
log.Fatal(err)
return
} else {
fmt.Println("Wrote to ", BoxAddr, " ", n, " Bytes: ", string(b[:n]))
}
time.Sleep(1000 * time.Millisecond)
}
}