问题
Here is my code:
package main
import (
"fmt"
"net"
)
func main() {
addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:8081")
listener, _ := net.ListenTCP("tcp", addr)
fmt.Printf("listener addr: %s\n", listener.Addr().String())
for {
conn, err := listener.AcceptTCP()
if err != nil {
// handle error
fmt.Println("err")
return
}
go handleConnection(conn)
}
}
func handleConnection(conn *net.TCPConn) {
fmt.Printf("conn addr: %s\n", conn.LocalAddr().String())
fmt.Printf("conn remote addr: %s\n", conn.RemoteAddr().String())
}
Output
listener addr: 127.0.0.1:8081
conn addr: 127.0.0.1:8081
conn remote addr: 127.0.0.1:1234
Why do listener
and conn
have the same address? In TCP, I thought a new socket was created for new connections.
回答1:
It got me puzzled for a second, but this is correct. A new socket is indeed created (with a unique local+remote address tuple). This quote from wikipedia describes it pretty well:
A server may create several concurrently established TCP sockets with the same local port number and local IP address, each mapped to its own server-child process, serving its own client process. They are treated as different sockets by the operating system, since the remote socket address (the client IP address and/or port number) are different; i.e. since they have different socket pair tuples.
If you think about it other way around, i.e. outgoing connection, you don't find it strange seeing remote address being the same across many sockets (e.g. google.com:80
), so the same holds for incoming connections.
Probably a nice side effect of this is that tools like netstat
, when inspecting sockets, are nicely showing the source port, instead of random pairs.
回答2:
No, your Listener is accepting connections in port 8081, so the LocalAddr
is going to have that port. If you dial out to another server, you usually use a different port each time, though that's not required either.
来源:https://stackoverflow.com/questions/28798964/tcp-connections-in-go