Go中的TCP连接 [英] TCP Connections in Go

查看:329
本文介绍了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\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())               
}  

输出

listener addr: 127.0.0.1:8081
conn addr: 127.0.0.1:8081
conn remote addr: 127.0.0.1:1234

为什么侦听器 conn 具有相同的地址?在TCP中,我认为为新的连接创建了一个新的套接字。

Why do listener and conn have the same address? In TCP, I thought a new socket was created for new connections.

推荐答案


服务器可以创建几个同时建立的具有相同本地端口号和本地IP地址的TCP套接字,每个映射到它自己的服务器子进程,为它自己的客户进程提供服务。由于远程套接字地址(客户端IP地址和/或端口号)不同,它们被操作系统视为不同的套接字;即,因为它们具有不同的套接字对元组。

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.

如果您想过其他方法,即传出连接,您不会找到奇怪的是,在许多套接字中看到远程地址是相同的(例如 google.com:80 ),所以对于传入连接也是如此。

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.

这可能是一个很好的副作用,当检查套接字时,像 netstat 这样的工具很好地显示源端口,而不是随机对。

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.

这篇关于Go中的TCP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆