在套接字编程中,我看到服务器绑定(bind)到一个地址——由 IP 和端口号组成。然后是收听和接受(阻塞调用)的步骤。我想知道为什么客户端不需要绑定(bind)它的 ip 地址和端口号。实际上,它与服务器套接字地址绑定(bind)。之后,客户端调用 connect() 调用来建立连接。稍后,客户端和服务器都可以相互交谈。服务器如何与客户端对话?客户端未绑定(bind)其 IP 地址和端口号。服务器如何到达客户端?
最佳答案
I am wondering why the client doesn't need to bind with its ip address and port number.
因为有一个内部 bind() 作为 connect() 的一部分,如果套接字尚未绑定(bind),并且因为服务器不关心客户端的端口号是什么:它不需要固定比如服务器的端口号。
In fact, it binds with the server socket address.
不,它没有:它连接到服务器的地址。
How a server can speak to the client?
通过已建立的连接。
The client has not bind with its IP address and port number.
是的,见上文。
How is it possible for a server to reach client?
客户端到达服务器的方式相同。通过发送带有目标地址和端口的数据包。
关于linux - 在套接字编程中,为什么客户端不绑定(bind)地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21645970/