当前位置: 首页>>代码示例>>Golang>>正文


Golang UDPConn.WriteTo方法代码示例

本文整理汇总了Golang中net.UDPConn.WriteTo方法的典型用法代码示例。如果您正苦于以下问题:Golang UDPConn.WriteTo方法的具体用法?Golang UDPConn.WriteTo怎么用?Golang UDPConn.WriteTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net.UDPConn的用法示例。


在下文中一共展示了UDPConn.WriteTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: HandleVOIPData

func (tunnel *Tunnel) HandleVOIPData(buff []byte, addr *net.UDPAddr, conn *net.UDPConn) {
	now := time.Now().Unix()

	_, receiver, _, err := tunnel.ReadVOIPData(buff)
	if err != nil {
		return
	}

	client := tunnel.FindClient(addr)
	if client == nil || client.appid == 0 {
		return
	}

	client.timestamp = now
	//转发消息
	other := tunnel.FindAppClient(client.appid, receiver)
	if other == nil {
		log.Infof("can't dispatch voip data sender:%d receiver:%d", client.uid, receiver)
		return
	}

	if other.has_header {
		buffer := new(bytes.Buffer)
		var h byte = VOIP_DATA
		buffer.WriteByte(h)
		buffer.Write(buff)
		data := buffer.Bytes()
		conn.WriteTo(data, other.addr)
	} else {
		data := buff
		conn.WriteTo(data, other.addr)
	}
}
开发者ID:931743010,项目名称:voip_service,代码行数:33,代码来源:tunnel.go

示例2: main

func main() {

	go func() {
		var addr net.Addr
		var udp *net.UDPConn
		var err error
		if addr, err = net.ResolveUDPAddr("udp4", ":1234"); err != nil {
			panic(err)
		}
		if udp, err = net.ListenUDP("udp", addr.(*net.UDPAddr)); err != nil {
			panic(err)
		}
		var buf = make([]byte, 65535)
		n, addr, e := udp.ReadFrom(buf)
		if e != nil {
			panic(e)
		}
		udp.WriteTo(buf[:n], addr)
	}()
	runtime.Gosched()
	svr := NewPortalSvr()
	svr.basAddr = "127.0.0.1:1234"
	svr.basSecret = []byte("xxffe")
	if err := svr.Listen(":1288"); err != nil {
		panic(err)
	}
	svr.Run()
	err := svr.Auth("aas", "asas1", "255.255.255.255:1223")
	if err != nil {
		println(err.Error())
	}
}
开发者ID:Joinhack,项目名称:fragement,代码行数:32,代码来源:main.go

示例3: HandleAuth

func (tunnel *Tunnel) HandleAuth(buff []byte, addr *net.UDPAddr, conn *net.UDPConn) {
	now := time.Now().Unix()
	token, err := tunnel.ReadVOIPAuth(buff)
	if err != nil {
		return
	}
	client := tunnel.FindClient(addr)
	if client == nil {
		//首次收到认证消息
		client = &TunnelClient{appid: 0, uid: 0, addr: addr, timestamp: now, has_header: true, token: token}
		tunnel.AuthClient(client, token)
		return
	} else if client.token == token {
		//认证成功
		t := make([]byte, 2)
		t[0] = VOIP_AUTH_STATUS
		t[1] = 0
		conn.WriteTo(t, addr)
		client.timestamp = now
		log.Infof("tunnel auth appid:%d uid:%d", client.appid, client.uid)
	} else {
		//新的token
		tunnel.RemoveTunnelClient(client)
		client.token = token
		client.appid = 0
		client.uid = 0
		client.timestamp = now
		tunnel.AuthClient(client, token)
	}
}
开发者ID:931743010,项目名称:voip_service,代码行数:30,代码来源:tunnel.go

示例4: GetSend

func GetSend(conn *net.UDPConn, context *UdpContext) {
	file := context.file
	buffer := make([]byte, 1024)
	// Read into buffer
	len, err := file.Read(buffer)

	if len > 0 {
		// Write to socket
		_, err = conn.WriteTo(buffer[:len], context.dest)
		if err != nil {
			log.Println("Failed to write data to socket; abandoning transfer")
		}
	}

	// EOF?
	if err == io.EOF {
		log.Println("Transfer completed")
		return
	}

	// Some other error
	if err != nil {
		log.Println("Error reading file; abandoning transfer")
		return
	}
}
开发者ID:parthp022,项目名称:GoFtpServer,代码行数:26,代码来源:server.go

示例5: sendAnnouncement

func (d *UDPClient) sendAnnouncement(remote net.Addr, conn *net.UDPConn) bool {
	if debug {
		l.Debugf("discover %s: broadcast: Sending self announcement to %v", d.url, remote)
	}

	ann := d.announcer.Announcement()
	pkt, err := ann.MarshalXDR()
	if err != nil {
		return false
	}

	myID := protocol.DeviceIDFromBytes(ann.This.ID)

	_, err = conn.WriteTo(pkt, remote)
	if err != nil {
		if debug {
			l.Debugf("discover %s: broadcast: Failed to send self announcement: %s", d.url, err)
		}
		return false
	}

	// Verify that the announce server responds positively for our device ID

	time.Sleep(1 * time.Second)

	ann, err = d.Lookup(myID)
	if err != nil && debug {
		l.Debugf("discover %s: broadcast: Self-lookup failed: %v", d.url, err)
	} else if debug {
		l.Debugf("discover %s: broadcast: Self-lookup returned: %v", d.url, ann.This.Addresses)
	}
	return len(ann.This.Addresses) > 0
}
开发者ID:rwx-zwx-awx,项目名称:syncthing,代码行数:33,代码来源:client_udp.go

示例6: handleudp

func handleudp(conn *net.UDPConn, remaddr net.Addr, message []byte) {
	fmt.Printf("UDP packet from %s (to %s)... ", remaddr, conn.LocalAddr())
	n, error := conn.WriteTo(message, remaddr)
	checkError("Cannot write", error)
	if n != len(message) {
		panic("Cannot write")
	}
	fmt.Printf("Echoed %d bytes\n", n)
}
开发者ID:bortzmeyer,项目名称:nonewcontent,代码行数:9,代码来源:nonewcontent.go

示例7: HandleUdpGet

func HandleUdpGet(conn *net.UDPConn, context *UdpContext) {

	log.Printf("Request for file '%s' (UDP)", context.parameters)

	command := fmt.Sprintf("length: %d\n", context.length)
	conn.WriteTo([]byte(command), context.dest)

	GetSend(conn, context)
}
开发者ID:parthp022,项目名称:GoFtpServer,代码行数:9,代码来源:server.go

示例8: TestHandlePing

func TestHandlePing(t *testing.T) {
	m := GetMemberlist(t)
	m.config.EnableCompression = false
	defer m.Shutdown()

	var udp *net.UDPConn
	for port := 60000; port < 61000; port++ {
		udpAddr := fmt.Sprintf("127.0.0.1:%d", port)
		udpLn, err := net.ListenPacket("udp", udpAddr)
		if err == nil {
			udp = udpLn.(*net.UDPConn)
			break
		}
	}

	if udp == nil {
		t.Fatalf("no udp listener")
	}

	// Encode a ping
	ping := ping{SeqNo: 42}
	buf, err := encode(pingMsg, ping)
	if err != nil {
		t.Fatalf("unexpected err %s", err)
	}

	// Send
	addr := &net.UDPAddr{IP: net.ParseIP(m.config.BindAddr), Port: m.config.BindPort}
	udp.WriteTo(buf.Bytes(), addr)

	// Wait for response
	go func() {
		time.Sleep(time.Second)
		panic("timeout")
	}()

	in := make([]byte, 1500)
	n, _, err := udp.ReadFrom(in)
	if err != nil {
		t.Fatalf("unexpected err %s", err)
	}
	in = in[0:n]

	msgType := messageType(in[0])
	if msgType != ackRespMsg {
		t.Fatalf("bad response %v", in)
	}

	var ack ackResp
	if err := decode(in[1:], &ack); err != nil {
		t.Fatalf("unexpected err %s", err)
	}

	if ack.SeqNo != 42 {
		t.Fatalf("bad sequence no")
	}
}
开发者ID:rlayte,项目名称:memberlist,代码行数:57,代码来源:net_test.go

示例9: udpEchoHandler

func udpEchoHandler(c *net.UDPConn) {
	buf := make([]byte, 64)
	for {
		n, addr, err := c.ReadFrom(buf)
		if err != nil {
			return
		}
		if _, err = c.WriteTo(buf[0:n], addr); err != nil {
			return
		}
	}
}
开发者ID:Cepave,项目名称:lvs-metrics,代码行数:12,代码来源:healthcheck_test.go

示例10: respond

// Listens for an incoming packet and replies to it.
func respond(t *testing.T, conn *net.UDPConn) {
	buff := make([]byte, 16)
	_, addr, err := conn.ReadFromUDP(buff)
	if err != nil {
		t.Fatalf("Cannot serve request: %s", err)
	}

	response := []byte("Some reply")
	if _, err = conn.WriteTo(response, addr); err != nil {
		t.Fatalf("Cannot write response: %s", err)
	}
}
开发者ID:vonwenm,项目名称:gossip,代码行数:13,代码来源:broadcast_test.go

示例11: Send

/*
Send package
*/
func (pack *Package) Send(conn *net.UDPConn) (n int, err error) {
	pack.DataSize = uint16(len(pack.Data))
	buf := new(bytes.Buffer)
	err = binary.Write(buf, binary.LittleEndian, pack.SeqID)
	if err != nil {
		return
	}
	if pack.IsResponse {
		t := uint16(uint16(0X8000) | pack.MsgID)
		err = binary.Write(buf, binary.LittleEndian, t)
		if err != nil {
			return
		}
	} else {
		err = binary.Write(buf, binary.LittleEndian, pack.MsgID)
		if err != nil {
			return
		}
	}
	err = binary.Write(buf, binary.LittleEndian, pack.DataSize)
	if err != nil {
		return
	}
	if pack.DataSize > 0 {
		if pack.IsResponse {
			log.Printf(">>> Response (%s): {MsgID:%d, SeqID:%d, Data:%s}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID, string(pack.Data))
		} else {
			log.Printf(">>> Request  (%s): {MsgID:%d, SeqID:%d, Data:%s}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID, string(pack.Data))
		}
		n, err = buf.Write(pack.Data)
		if err != nil {
			return
		}
		if n != int(pack.DataSize) {
			return 0, errors.New("Write buffer error")
		}
	} else {
		if pack.IsResponse {
			log.Printf(">>> Response (%s): {MsgID:%d, SeqID:%d, Data:nil}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID)
		} else {
			log.Printf(">>> Request  (%s): {MsgID:%d, SeqID:%d, Data:nil}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID)
		}
	}
	n, err = conn.WriteTo(buf.Bytes(), pack.Addr)
	if err != nil {
		return
	}
	if n != int(pack.DataSize+PACKAGE_HEAD_LEN) {
		return n, errors.New("Send failed!")
	}
	return
}
开发者ID:todostreaming,项目名称:gop2p,代码行数:55,代码来源:protocol.go

示例12: Transmit

// Transmit a message.
func Transmit(l *net.UDPConn, a *net.UDPAddr, m Message) error {
	d, err := m.encode()
	if err != nil {
		return err
	}

	if a == nil {
		_, err = l.Write(d)
	} else {
		_, err = l.WriteTo(d, a)
	}
	return err
}
开发者ID:jvermillard,项目名称:go-coap,代码行数:14,代码来源:server.go

示例13: handleReq

func handleReq(n int, addr net.Addr, buf []byte, sCon *net.UDPConn){
	if n < 2 {
		log.Printf("%v Sent less than 2 bytes\n", addr)
		return
	}
	pId := binary.BigEndian.Uint16(buf[:2])
	switch {
	case pId == 0 && n == 2: //Public key request
		if _,err := sCon.WriteTo(encryption.RsaPub, addr); err != nil {
			log.Printf("Failed to reply to %v : %v", addr, err)
		}
	case pId == 0 && n > 2: //Registration
		ciphertext := buf[2:n]
		text, err := encryption.DecryptRSA(ciphertext)
		if err != nil{
			log.Printf("%v Incorrectly encrypted message (RSA)\n")
		}
		log.Println(text)
	case n >= 2*encryption.AES_LENGTH + 2 + encryption.HMAC_LENGTH: //Request
		pId-- //Switch to real pId
		if int(pId) >= len(data.Players) {
			print(pId)
			log.Printf("%v Sent an invalid player id\n", addr)
			return
		}
		key := data.Players[pId].Key
		text, err := encryption.Decrypt(buf[2:n], key)
		if err != nil {
			log.Printf("%v %s\n", addr, err)
			return
		}
		log.Printf("%v %d\n", addr, text)
		data.Players[pId].Addr = addr
		switch text[0] {
		case 1: //SET2
			if err := data.ReqSET2(text[1:], pId); err != nil {
				log.Printf("%v %s\n", addr, err)
			}
		default:
			log.Printf("%v Unknown request: %x", addr, text[0])
		}

		//Reply
		/*res := encryption.Encrypt([]byte{1,2,3,5,8},key)
		_, err = sCon.WriteTo(res, addr)
		checkWarn(err)*/

	default:
		log.Printf("%v Incorrectly encrypted message\n", addr)
	}
}
开发者ID:OctoTail,项目名称:Emperor,代码行数:51,代码来源:main.go

示例14: process

/*
 * Handle incoming DNS packet:
 * - Only handle requests with at least on query resource record and
 *   empty response resource records; skip all other packets.
 */
func process(conn *net.UDPConn, addr net.Addr, buf []byte, n int) {

	pos := 0
	id, pos := getShort(buf, pos)
	qtype, pos := getShort(buf, pos)
	if (qtype & 0x8000) != 0 {
		if verbose {
			fmt.Printf("[Tor-DNS] Request is not a standard query: %x\n", qtype)
		}
		return
	}
	mode := (qtype >> 11) & 0xF
	if mode > 1 {
		if verbose {
			fmt.Printf("[Tor-DNS] Request is not a standard type: %x\n", qtype)
		}
		return
	}

	qd_cnt, pos := getShort(buf, pos)
	an_cnt, pos := getShort(buf, pos)
	ns_cnt, pos := getShort(buf, pos)
	ar_cnt, pos := getShort(buf, pos)

	if an_cnt > 0 || ns_cnt > 0 || ar_cnt > 0 || qd_cnt != 1 {
		if verbose {
			fmt.Printf("[Tor-DNS] Request has invalid counts: (%d,%d,%d,%d)\n", qd_cnt, an_cnt, ns_cnt, ar_cnt)
		}
		return
	}

	var q query
	q.mode = mode
	name, num := read_name(buf[pos:])
	q.name = name
	pos += num
	q.typ, pos = getShort(buf, pos)
	q.class, pos = getShort(buf, pos)

	r, err := answer(id, q)
	if err != nil {
		fmt.Printf("[Tor-DNS] Can't answer query %x: %s\n", id, err.Error())
		return
	}
	if n = assemble(buf, id, r); n > 0 {
		conn.WriteTo(buf[:n], addr)
	}
}
开发者ID:johnjohnsp1,项目名称:Tor-DNS,代码行数:53,代码来源:main.go

示例15: cliTunnelUDP

func cliTunnelUDP(uconn *net.UDPConn, sconn net.Conn) {
	var raddr *net.UDPAddr

	go func() {
		b := lpool.Take()
		defer lpool.put(b)

		for {
			n, addr, err := uconn.ReadFromUDP(b)
			if err != nil {
				log.Println(err)
				return
			}
			raddr = addr
			r := bytes.NewBuffer(b[:n])
			udp, err := gosocks5.ReadUDPDatagram(r)
			if err != nil {
				return
			}
			udp.Header.Rsv = uint16(len(udp.Data))
			//log.Println("r", raddr.String(), udp.Header)

			if err := udp.Write(sconn); err != nil {
				log.Println(err)
				return
			}
		}
	}()

	for {
		b := lpool.Take()
		defer lpool.put(b)

		udp, err := gosocks5.ReadUDPDatagram(sconn)
		if err != nil {
			log.Println(err)
			return
		}
		//log.Println("w", udp.Header)
		udp.Header.Rsv = 0
		buf := bytes.NewBuffer(b[0:0])
		udp.Write(buf)
		if _, err := uconn.WriteTo(buf.Bytes(), raddr); err != nil {
			log.Println(err)
			return
		}
	}
}
开发者ID:Weeker,项目名称:gost,代码行数:48,代码来源:client.go


注:本文中的net.UDPConn.WriteTo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。