Hi

I think you wanted return ahost == bhost (instead of aport == bport) inside 
the addrCmp function, right ? Because the client is always assigned a 
random port for a TCP connection 

And of course, you will have to compare at nc (net.Conn) level, not at 
listener level, especially when listening on a generic [::]

e.g.
go func() {
for {
nc, err := l.Accept()
if err != nil {
log.Fatal("accept fatal: ", err)
}
// This is the address you need to compare with
connectionSpecificAddr := nc.LocalAddr().String()
connRemoteAddr := nc.RemoteAddr().String()
fmt.Println("Listener local addr: ", connectionSpecificAddr)
fmt.Println("Listener remote addr: ", connRemoteAddr)
fmt.Println(addrCmp(connectionSpecificAddr, connRemoteAddr))

}
}()

Cheers,
Silviu



On Saturday, 3 June 2017 23:29:24 UTC-4, Константин Иванов wrote:
>
> Here is a code:
>
> package main
>
> import (
>     "fmt"
>     "log"
>     "net"
> )
>
> func main() {
>
>     l, err := net.Listen("tcp", "[::]:9090")
>     if err != nil {
>         log.Fatal(err)
>     }
>     defer l.Close()
>
>     go func() {
>         for {
>             l.Accept()
>         }
>     }()
>
>     c, err := net.Dial("tcp", "[::]:9090")
>     if err != nil {
>         log.Fatal(err)
>     }
>     defer c.Close()
>
>     fmt.Println(l.Addr().String())
>     fmt.Println(c.RemoteAddr().String())
>
>     fmt.Println(addrCmp(l.Addr().String(), c.RemoteAddr().String()))
> }
>
> func addrCmp(a, b string) bool {
>     if ahost, aport, err := net.SplitHostPort(a); err == nil {
>         if bhost, bport, err := net.SplitHostPort(b); err == nil {
>             if net.ParseIP(ahost).Equal(net.ParseIP(bhost)) {
>                 return aport == bport
>             }
>         }
>     }
>     return false
> }
>
> Output is:
>
> [::]:9090
> [::1]:9090
> false
>
> Is there an easy way to compare two net.Addr or two strings that 
> represents network addresses?
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to