本文整理汇总了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)
}
}
示例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
}
}
}
}
示例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)
}
}
示例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
}
示例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()
}
示例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
}
示例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
}
示例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)
}
}
示例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)
}
}
示例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
}
示例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)
}
}
示例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
}
示例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
}
}
}
示例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)
}
}
示例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)
}