如果从一个未绑定(bind)的套接字读取会发生什么?更具体地说是在 UDP 套接字上?
如果套接字已用于发送信息,则套接字将绑定(bind)到端口(和地址),因此我们将监听该端口(地址,端口),但是如果套接字完全未绑定(bind),那么预期的行为是什么?
在这种情况下,该套接字将如何响应 select/epoll 以进行读取操作?
最佳答案
它有各种指定或未指定。
从 man 7 udp (Linux):
In order to receive packets, the socket can be bound to a local address first by using bind(2). Otherwise the socket layer will automatically assign a free local port out of the range defined by
/proc/sys/net/ipv4/ip_local_port_range
and bind the socket to INADDR_ANY.
来自 MSDN
recv()
function :The local address of the socket must be known. For server applications, use an explicit bind function or an implicit
accept
orWSAAccept
function. Explicit binding is discouraged for client applications. For client applications, the socket can become bound implicitly to a local address usingconnect
,WSAConnect
,sendto
,WSASendTo
, orWSAJoinLeaf
.
和
WSAEINVAL: The socket has not been bound with bind, ...
我无法回答 BSD、Solaris、HP-UX、AIX、...
关于sockets - read() 形成一个未绑定(bind)的套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44741609/