sockets - 套接字编程 : bind() system call

标签 sockets networking

在研究计算机网络作为类(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.

我的问题是:
  • 当我们在编写客户端程序时通常不调用 bind() 系统调用时,操作系统如何自动选择端口号。
  • 当两个不同的应用程序使用相同的端口和 IP 组合连接到两个不同的服务器时,当这些服务器回复时,操作系统如何找出需要将哪个数据包重定向到哪个应用程序。
  • 最佳答案

    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_connecttcp_v4_connect .

    关于sockets - 套接字编程 : bind() system call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14424378/

    相关文章:

    python-2.7 - 使用python套接字为每个页面获取404错误

    Python,连接被拒绝 10061

    networking - net/http GET 请求错误 tls 收到长度为 20527 的超大记录

    python - 当我在 Windows 上使用 os.dup2() 时,出现错误 : OSError: [Errno 9] Bad file descriptor

    java - 通过 TCP 套接字发送数据包

    java - 如何使用套接字ping IP并通过它发送数据?

    java - 为基于 netty 的 EPPServer 添加 SSL 支持时,客户端 socket.startHandshake() 失败。

    c++ - udp源端口和目的端口必须匹配吗?

    C++ boost 单元测试 : How to manually finish unit test with success?

    java - 从 IO 迁移到 NIO - 网络、IllegalBlockingModeException