Golang net.IPConn.SetDeadline(), SetReadDeadline() and SetReadBuffer() functions example

package net

Golang net.IPConn.SetDeadline(), SetReadDeadline() and SetReadBuffer() functions usage example

 package main

 import (
 "fmt"
 "net"
 "time"
 )

 func main() {
 // replace x with your target IP address
 raddr := &net.IPAddr{IP: net.IPv4(x, x, x, x).To4()}
 laddr := &net.IPAddr{IP: net.IPv4(x, x, x, x)}

 ipconn, err := net.DialIP("ip:tcp", laddr, raddr)
 if err != nil {
 fmt.Println("Error: ", err)
 } else {
 fmt.Println(ipconn)
 }

 defer ipconn.Close()

 // the same can be applied to SetWriteDeadline() and SetWriteBuffer() functions as well
 t := time.Now()
 ipconn.SetDeadline(t.Add(100 * time.Millisecond))
 ipconn.SetReadDeadline(t.Add(250 * time.Millisecond))

 //buffer := make([]byte, 4096)

 err := ipconn.SetReadBuffer(4096)

 if err != nil {
 fmt.Println("Error: ", err)
 }
 }

References :

http://golang.org/pkg/net/#IPConn.SetDeadline

http://golang.org/pkg/net/#IPConn.SetReadDeadline

http://golang.org/pkg/net/#IPConn.SetReadBuffer

Advertisement