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


Golang PacketConn.WriteTo方法代碼示例

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


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

示例1: echoServer

func echoServer(Address *string, Message *string) {
	var c net.PacketConn
	var err error

	c, err = net.ListenPacket("sctp", *settings.Address)

	if err != nil {
		log.Printf("Error listening: %v", err)
		os.Exit(-1)
	}
	defer c.Close()

	for {
		msg := make([]byte, 2048)
		log.Printf("Listening on %s", *settings.Address)
		_, addr, err := c.ReadFrom(msg)
		if err != nil {
			log.Printf("Error: %v ", err)
			break
		}
		fmt.Println("Message: " + string(msg))
		c.WriteTo(msg, addr)
	}

}
開發者ID:cyberroadie,項目名稱:msc-report-src,代碼行數:25,代碼來源:EchoSimpleSCTPServer.go

示例2: serveTestUdp

func serveTestUdp(in net.PacketConn, pdu_txt string, waiter *sync.WaitGroup) {

	defer func() {
		waiter.Done()
	}()

	var byteArray [10000]byte

	for {
		_, addr, err := in.ReadFrom(byteArray[:])
		if nil != err {
			fmt.Println("[test] read failed", err.Error())
			break
		}

		fmt.Println("[test] recv ok, send at next step")

		bin, err := hex.DecodeString(pdu_txt)
		if nil != err {
			fmt.Println("[test]", err.Error())
		} else {
			if _, err = in.WriteTo(bin, addr); nil != err {
				fmt.Println("[test] write failed", err.Error())
				break
			}
		}
	}
}
開發者ID:runner-mei,項目名稱:snmpclient,代碼行數:28,代碼來源:udp_client_test.go

示例3: sendPacket

func sendPacket(conn net.PacketConn, destination *net.IPAddr, payload []byte, seq int) {
	packet := make([]byte, len(payload)+8)
	packet[0] = 8
	packet[1] = 0
	packet[4] = uint8(os.Getpid() >> 8)
	packet[5] = uint8(os.Getpid() & 0xff)
	packet[6] = uint8(seq >> 8)
	packet[7] = uint8(seq & 0xff)
	copy(packet[8:], payload)

	cklen := len(packet)
	cksum := uint32(0)
	for i := 0; i < cklen-1; i += 2 {
		cksum += uint32(packet[i+1])<<8 | uint32(packet[i])
	}
	if cklen&1 == 1 {
		cksum += uint32(packet[cklen-1])
	}
	cksum = (cksum >> 16) + (cksum & 0xffff)
	cksum = cksum + (cksum >> 16)

	packet[2] ^= uint8(^cksum & 0xff)
	packet[3] ^= uint8(^cksum >> 8)

	_, err := conn.WriteTo(packet, destination)
	if err != nil {
		panic(err)
	}
}
開發者ID:ragnar-johannsson,項目名稱:icmp-echo-file,代碼行數:29,代碼來源:icmp-echo-file.go

示例4: WriteFile

func WriteFile(w io.Writer, conn net.PacketConn, remoteAddress net.Addr, packet []byte, tid uint16) (int, net.Addr, error) {
	// Read data packet
	n, replyAddr, err := conn.ReadFrom(packet)
	if err != nil {
		return n, replyAddr, fmt.Errorf("Error reading packet: %v", err)
	}

	opcode, err := GetOpCode(packet)
	if err != nil {
		return n, replyAddr, fmt.Errorf("Error getting opcode: %v", err)
	}
	if opcode != OpDATA {
		return n, replyAddr, fmt.Errorf("Expected DATA packet, got %v\n", opcode)
	}

	packetTID := binary.BigEndian.Uint16(packet[2:4])
	if packetTID != tid {
		SendError(5, "Unknown transfer id", conn, remoteAddress)
		return n, replyAddr, fmt.Errorf("Expected TID %d, got %d\n", tid, packetTID)
	}

	// Write data to disk
	_, err = w.Write(packet[4:n])
	if err != nil {
		return n, replyAddr, fmt.Errorf("Error writing: %v", err)
	}

	ack := CreateAckPacket(tid)
	_, err = conn.WriteTo(ack, replyAddr)
	if err != nil {
		return n, replyAddr, fmt.Errorf("Error writing ACK packet: %v", err)
	}

	return n, replyAddr, nil
}
開發者ID:ryanslade,項目名稱:tftp,代碼行數:35,代碼來源:common.go

示例5: sendAbort

// sends an ABORT packet to the specified peer
func sendAbort(c net.PacketConn, addr net.Addr, tag []byte) {
	p := NewPacket(4)
	p.D[0] = byte(PacketAbort)
	copy(p.D[1:4], tag)
	c.WriteTo(p.D, addr)
	p.Free()
}
開發者ID:sunnyboy00,項目名稱:gomnet,代碼行數:8,代碼來源:listener.go

示例6: send

// RFC 3489: Clients SHOULD retransmit the request starting with an interval
// of 100ms, doubling every retransmit until the interval reaches 1.6s.
// Retransmissions continue with intervals of 1.6s until a response is
// received, or a total of 9 requests have been sent.
func (v *packet) send(conn net.PacketConn, addr net.Addr) (*packet, error) {
	timeout := 100
	for i := 0; i < 9; i++ {
		length, err := conn.WriteTo(v.bytes(), addr)
		if err != nil {
			return nil, err
		}
		if length != len(v.bytes()) {
			return nil, errors.New("Error in sending data.")
		}
		conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Millisecond))
		if timeout < 1600 {
			timeout *= 2
		}
		packetBytes := make([]byte, 1024)
		length, _, err = conn.ReadFrom(packetBytes)
		if err == nil {
			return newPacketFromBytes(packetBytes[0:length])
		} else {
			if !err.(net.Error).Timeout() {
				return nil, err
			}
		}
	}
	return nil, nil
}
開發者ID:skycoin,項目名稱:skycoin,代碼行數:30,代碼來源:packet.go

示例7: SendError

func SendError(code uint16, message string, conn net.PacketConn, remoteAddress net.Addr) error {
	errPacket := CreateErrorPacket(0, message)
	_, err := conn.WriteTo(errPacket, remoteAddress)
	if err != nil {
		return fmt.Errorf("Error writing error packet: %v", err)
	}
	return nil
}
開發者ID:ryanslade,項目名稱:tftp,代碼行數:8,代碼來源:common.go

示例8: benchmarkReadWriteNetUDP

func benchmarkReadWriteNetUDP(b *testing.B, c net.PacketConn, wb, rb []byte, dst net.Addr) {
	if _, err := c.WriteTo(wb, dst); err != nil {
		b.Fatal(err)
	}
	if _, _, err := c.ReadFrom(rb); err != nil {
		b.Fatal(err)
	}
}
開發者ID:musha68k,項目名稱:go-ipfs,代碼行數:8,代碼來源:readwrite_test.go

示例9: MGCPCommandRespondEcho

func MGCPCommandRespondEcho(msg MGCPCommand, conn net.PacketConn, addr net.Addr) {
	str_reply := fmt.Sprintf("%s", msg)

	if _, err := conn.WriteTo([]byte(str_reply), addr); err != nil {
		fmt.Println(err)
	}

}
開發者ID:rkday,項目名稱:go-mgcp,代碼行數:8,代碼來源:mgcp_serv.go

示例10: Send

func (p *Packet) Send(c net.PacketConn, addr net.Addr) error {
	buf, err := p.Encode()
	if err != nil {
		return err
	}

	_, err = c.WriteTo(buf, addr)
	return err
}
開發者ID:keysonZZZ,項目名稱:kmg,代碼行數:9,代碼來源:packet.go

示例11: benchmarkReadWriteNetUDP

func benchmarkReadWriteNetUDP(b *testing.B, c net.PacketConn, dst net.Addr) {
	if _, err := c.WriteTo([]byte("HELLO-R-U-THERE"), dst); err != nil {
		b.Fatalf("net.PacketConn.WriteTo failed: %v", err)
	}
	rb := make([]byte, 128)
	if _, _, err := c.ReadFrom(rb); err != nil {
		b.Fatalf("net.PacketConn.ReadFrom failed: %v", err)
	}
}
開發者ID:rocksolidlabs,項目名稱:go.net,代碼行數:9,代碼來源:unicast_test.go

示例12: Send

func (p *Packet) Send(c net.PacketConn, addr net.Addr) error {
	var buf [4096]byte
	n, _, err := p.Encode(buf[:])
	if err != nil {
		return err
	}

	n, err = c.WriteTo(buf[:n], addr)
	return err
}
開發者ID:karlpilkington,項目名稱:radius-1,代碼行數:10,代碼來源:packet.go

示例13: packetEcho

func packetEcho(c net.PacketConn) {
	defer c.Close()
	buf := make([]byte, 65536)
	for {
		n, addr, err := c.ReadFrom(buf)
		if err != nil {
			return
		}
		if _, err := c.WriteTo(buf[:n], addr); err != nil {
			return
		}
	}
}
開發者ID:uin57,項目名稱:frontd,代碼行數:13,代碼來源:reuse_test.go

示例14: MGCPCommandRespondErr

func MGCPCommandRespondErr(msg MGCPCommand, conn net.PacketConn, addr net.Addr) {
	var reply MGCPResponse
	reply.ResponseCode = "504"
	reply.TransID = msg.TransID
	reply.ResponseStr = "ERR"

	str_reply := fmt.Sprintf("%s", reply)

	if _, err := conn.WriteTo([]byte(str_reply), addr); err != nil {
		fmt.Println(err)
	}

}
開發者ID:rkday,項目名稱:go-mgcp,代碼行數:13,代碼來源:mgcp_serv.go

示例15: handleClient

func handleClient(conn net.PacketConn) {
	var buf [512]byte

	_, addr, err := conn.ReadFrom(buf[0:])

	if err != nil {
		return
	}

	daytime := time.Now()

	msg := fmt.Sprint(daytime)

	conn.WriteTo([]byte(msg), addr)
}
開發者ID:ngpeijiun,項目名稱:sample-code-go,代碼行數:15,代碼來源:UDPDaytimeServer2.go


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