首页 文章

套接字,其属性和SO_REUSEADDR选项

提问于
浏览
1

我有几个基本问题:

1.套接字由协议,本地IP,本地端口,远程ip和远程端口表示 . 假设客户端和服务器之间存在这种连接 . 现在当 i bind another client to same local port and ip 时,它被绑定(我使用了SO_REUSEADDR)但是将第二个客户端的操作连接到同一个远程ip和端口失败 . 所以, is there no way a third process can share the same socket?

2.当我们在绑定到本地端口和ip的套接字上调用listen()时,它会侦听连接 . 当客户端连接时,它会创建一个套接字(比如说A) . 它完成3路握手然后启动一个不同的套接字(比如B)并删除套接字A(Source) . 新的套接字由新的套接字B处理 . 所以, what kind of a socket represents a listening socket i.e. what is the remote ip and port and is socket A different than that socket or just addition of remote ip and port to listening socket forms A?

我读到了 SO_REUSEADDR can establish a listening socket on a port if there is no socket listening on that port and ip and all sockets on that port and ip have SO_REUSEADDR option set . 但是后来我也遇到了一个说 if a client is bound to a port and ip, another client can't bind to it(even if SO_REUSEADDR is used) unless the first client successfully calls connect() 的文字 . 有 no listening socket (它是一个客户端,所以我们没有调用connect())在该端口和ip in this example 上 . 所以, why isn't another client allowed?

提前致谢 .

1 回答

  • 1
    • 正确:无法使用相同的协议,本地端口,本地地址,远程端口和远程地址创建两个不同的套接字 . 没有什么可以分辨出哪个数据包属于哪个套接字!

    • 侦听套接字没有远程地址和远程端口 . 没关系,因为与此套接字关联的线路上没有数据包(尚未) . 实际上,所有套接字都没有本地或远程地址或端口 . 仅在稍后调用 bind() (对于本地)和 connect() / accept() (对于远程)时才分配这些属性 .

    • 直到你在套接字上调用 connect()listen() ,没有't any different between a server (listening) or client socket. They'同样的事情 . 因此,如果两个套接字都没有远程地址或端口,则不允许两个套接字共享相同的协议,本地地址和本地端口 .

    这不是在客户端套接字上调用 bind() ,这意味着在 connect() 时间有一个隐含 bind() 到临时端口 . 这些典型的客户端套接字不能与侦听套接字冲突,因为它们从没有与之关联的地址变为具有与它们相关联的本地和远程地址,跳过它们仅具有本地地址的状态 .

相关问题