我的问题基于以下理解
- 套接字由 ip:port 定义。服务器和客户端都有自己的套接字
- Socket连接由五组server_ip、server_port、client_ip、client_port、protocol定义
- 套接字描述符是标识套接字的整数值
我尝试实现自己的服务器,其中套接字()和接受()返回不同的套接字描述符值(总是这样吗?)。我的问题是为什么说如果服务器中没有打开新端口并且 ip:port 对于 socket() 和accept() 返回的套接字描述符相同,accept() 会创建一个新套接字。如果新的套接字是由accept()创建的,它与socket()创建的套接字有何不同?
I tried to implement my own server where socket()
and accept()
returns
different socket descriptor value (Is it always the case?).
Yes.
My question is why is it said that accept()
creates a new socket if no
new port is opened in server and ip:port is same for both the socket
descriptors returned by socket()
and accept()
. If new socket is
created by accept()
how is it different than the socket created by
socket()
?
因为第一个套接字用于等待通信,而第二个套接字用于通信。致电socket
(+bind
+listen
)准备一个通信端点,又名socket (or 服务器套接字) 接听来电。致电accept
在一个准备好的通信点上,等待来电,当发生这种情况时,会创建一个由以下表示的通信通道(2个端点+协议)连接的套接字由调用返回。
C API 可能会让您感到困惑,因为两者都称为套接字,但实际上用途不同。在一些其他语言/API 中进行了区分。例如在 Java 中你有ServerSocket
用于等待来电,以及Socket
用于通信的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)