在研究计算机网络作为类(class)主题时,我的概念是操作系统根据目标端口区分数据包并将其传递给在该端口上运行的应用程序。
后来我才知道我们可以使用同一个源(SourceIP:SourcePort)连接到两个不同的目的地(DestinationIP:DestinationPort)。
tcp 0 0 192.168.1.5:60000 199.7.57.72:80 ESTABLISHED 1000 196102 10179/firefox
tcp 0 0 192.168.1.5:60000 69.192.3.67:443 ESTABLISHED 1000 200361 10179/firefox
tcp 0 0 192.168.1.5:60000 69.171.234.18:80 ESTABLISHED 1000 196107 10179/firefox
tcp 0 0 192.168.1.5:60000 107.21.19.182:22 ESTABLISHED 1000 196399 10722/ssh
tcp 0 0 192.168.1.5:60000 69.171.234.18:443 ESTABLISHED 1000 201792 10179/firefox
tcp 0 0 192.168.1.5:60000 69.192.3.34:443 ESTABLISHED 1000 200349 10179/firefox
tcp 0 0 127.0.0.1:4369 127.0.0.1:51889 ESTABLISHED 129 12036 1649/epmd
tcp 0 0 192.168.1.5:60000 69.192.3.58:443 ESTABLISHED 1000 200352 10179/firefox
tcp 0 0 192.168.1.5:60000 74.125.236.88:80 ESTABLISHED 1000 200143 10179/firefox
tcp 0 0 192.168.1.5:60000 174.122.92.78:80 ESTABLISHED 1000 202935 10500/telnet
tcp 0 0 192.168.1.5:60000 74.125.236.87:80 ESTABLISHED 1000 201600 10179/firefox
再深入一点,我开始知道如果应用程序使用 bind() 系统调用将套接字描述符与特定的 IP 和端口组合绑定(bind),那么我们就不能再次使用相同的端口。否则,如果端口未绑定(bind)到任何套接字描述符,我们可以再次使用相同的端口和 IP 组合连接到不同的目标。
我在 bind() 系统调用的手册页中读到
bind() assigns the address specified to by addr to the socket referred to by the file descriptor sockfd.
我的问题是:
最佳答案
When we don't call bind() syscall generally while writing a client program then how does the OS automatically selects the port number
操作系统会随机选择一个未使用的端口(不一定是“下一个”端口)。
how does the OS finds out that which packet needs to be redirected to which application
每个 TCP 连接由一个 4 元组标识:(src_addr, src_port, dst_addr, dst_port),它是唯一的,因此足以识别每个段所属的位置。
编辑
When we don't call bind() syscall generally while writing a client program then how does the OS automatically selects the port number.
在 TCP 套接字的情况下,在“连接”之前的某个时间。比如Linux有函数
inet_csk_get_port
获取未使用的端口号。寻找 inet_hash_connect
在 tcp_v4_connect
.
关于sockets - 套接字编程 : bind() system call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14424378/