本文整理汇总了Golang中net.TCPConn.SetTimeout方法的典型用法代码示例。如果您正苦于以下问题:Golang TCPConn.SetTimeout方法的具体用法?Golang TCPConn.SetTimeout怎么用?Golang TCPConn.SetTimeout使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.TCPConn
的用法示例。
在下文中一共展示了TCPConn.SetTimeout方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleConnection
func handleConnection(listenConn *net.TCPConn) {
listenConn.SetReadBuffer(INBUFSIZE)
listenConn.SetTimeout(TIMEOUT)
bufioReader := bufio.NewReader(listenConn)
bufioWriter := bufio.NewWriter(listenConn)
addr := listenConn.RemoteAddr()
fmt.Printf("\nnetwork: %s\n", addr.Network())
fmt.Printf("\naddr: %s\n", addr.String())
for {
fmt.Printf("\nSCHLEIFE\n")
request, e := bufioReader.ReadBytes('!')
if e != nil {
break
}
requestLines := strings.Split(string(request), ";", 0)
length := len(requestLines)
if length > 0 {
length--
}
requestLines = requestLines[0:length]
for i, k := range requestLines {
fmt.Printf("requestLines[%d]: \"%s\"\n", i, k)
}
infos := parseCommand(len(requestLines), requestLines)
fmt.Printf("\nINFOS:\n")
for i, k := range infos {
fmt.Printf("\ninfos[%d]\"%s\"\n", i, k)
}
answer := strings.Join(infos, ";")
answer = requestLines[0] + ";" + answer + ";!"
fmt.Printf("\nSending: \"%s\"\n", answer)
bufioWriter.WriteString(answer)
bufioWriter.Flush()
fmt.Printf("\nSENT\n")
}
listenConn.Close()
}
示例2: NewUpstream
func NewUpstream(host string, port int, forceHttp bool) *Upstream {
u := new(Upstream)
u.Host = host
u.Port = port
u.ForceHttp = forceHttp
ips, err := net.LookupIP(host)
var ip net.IP = nil
for i := range ips {
ip = ips[i].To4()
if ip != nil {
break
}
}
if err == nil && ip != nil {
u.tcpaddr = new(net.TCPAddr)
u.tcpaddr.Port = port
u.tcpaddr.IP = ip
} else {
falcore.Warn("Can't get IP addr for %v: %v", host, err)
}
u.Timeout = 60e9
u.host = fmt.Sprintf("%v:%v", u.Host, u.Port)
u.transport = new(http.Transport)
u.transport.Dial = func(n, addr string) (c net.Conn, err os.Error) {
falcore.Fine("Dialing connection to %v", u.tcpaddr)
var ctcp *net.TCPConn
ctcp, err = net.DialTCP("tcp4", nil, u.tcpaddr)
if ctcp != nil {
ctcp.SetTimeout(u.Timeout)
}
if err != nil {
falcore.Error("Dial Failed: %v", err)
}
return ctcp, err
}
u.transport.MaxIdleConnsPerHost = 15
return u
}