dongluni0568 2015-03-01 20:55
浏览 101
已采纳

Go中的TCP连接

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
", 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
", conn.LocalAddr().String())                       
    fmt.Printf("conn remote addr: %s
", 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.

  • 写回答

2条回答 默认 最新

  • dqwh1202 2015-03-01 22:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多