Created
May 25, 2018 20:59
-
-
Save udhos/baf731d521c9be7f16a4ea7f38022ae4 to your computer and use it in GitHub Desktop.
golang_tcplistener_setdeadline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net" | |
"time" | |
) | |
func main() { | |
ln, errListen := net.Listen("tcp", "localhost:8000") | |
if errListen != nil { | |
log.Fatalf("Listen: %v", errListen) | |
} | |
t := ln.(*net.TCPListener) | |
now := time.Now() | |
deadline := now.Add(time.Second) | |
log.Printf("now: %v -- deadline: %v", now, deadline) | |
if errDeadline := t.SetDeadline(deadline); errDeadline != nil { | |
log.Fatalf("Setdeadline: %v", errListen) | |
} | |
conn, errAccept := t.Accept() | |
if errAccept != nil { | |
log.Printf("accept: %v", errAccept) | |
return | |
} | |
log.Printf("new connection: %v", conn) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment