TCP Connections in Go

醉酒当歌 提交于 2019-12-25 03:17:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!