Skip to content

Instantly share code, notes, and snippets.

@dat-boris
Last active April 21, 2021 17:17
Show Gist options
  • Save dat-boris/51f42ed243f4af69e572ed10d2370b69 to your computer and use it in GitHub Desktop.
Save dat-boris/51f42ed243f4af69e572ed10d2370b69 to your computer and use it in GitHub Desktop.
Looking at difference between port listening with/ without loopback address
package main
import (
"fmt"
"os"
"net"
"log"
"time"
)
func main() {
port := os.Args[1]
withAddr, _ := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%s", port))
withoutAddr, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
if err != nil {
log.Panic("%v", err)
}
fmt.Printf("With address: %v\n", withAddr)
fmt.Printf("Without address: %v\n", withoutAddr)
fmt.Println("Sleeping for 100 secs...")
time.Sleep(100 * time.Second)
if withAddr != nil {
withAddr.Close()
}
if withoutAddr != nil {
withoutAddr.Close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment