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
Advertisement
Something interesting
Tutorials
+26.7k Golang : How to check if a connection to database is still alive ?
+20.6k Nginx + FastCGI + Go Setup.
+9.4k Facebook : Getting the friends list with PHP return JSON format
+18.1k Golang : Check if a directory exist or not
+11.3k Golang : How to use if, eq and print properly in html template
+9k Golang : automatically figure out array length(size) with three dots
+8.3k Golang: Prevent over writing file with md5 hash
+33k Golang : How to check if a date is within certain range?
+8.2k Golang : Add build version and other information in executables
+18k Golang : Get all upper case or lower case characters from string example
+13.1k Golang : Handle or parse date string with Z suffix(RFC3339) example
+36.5k Golang : Save image to PNG, JPEG or GIF format.