简体   繁体   English

如果我想使用 UDP 从服务器接收数据,是否需要 bind()?

[英]Is bind() necessary if I want to receive data from a server using UDP?

Is it necessary to have in the client bind , if I want to receive data from a server?如果我想从服务器接收数据,是否需要在客户端bind

I've seen a schema (look at the image below), and it seems that it's not necessary.我看过一个模式(看下图),似乎没有必要。 But from what I know, bind is necessary to give an address to a socket.但据我所知,绑定是为套接字提供地址所必需的。 If I don't "bind" the socket, how can I send data to it?如果我不“绑定”套接字,我如何向它发送数据?

UDP客户端/服务器

A call to bind() only binds a socket to a port.bind()调用仅将套接字绑定到端口。 Hence, in this case, it is required for a server to bind to a port.因此,在这种情况下, server需要绑定到端口。

A client, on the other hand, just needs to send to or receive data from a specific port (server);另一方面,客户端只需要向特定端口(服务器)发送或接收数据; hence, it just connect() s and then does a recvfrom() and sendto() .因此,它只是connect() s,然后执行recvfrom()sendto()

Read last paragraph of section 5.3: http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html阅读第 5.3 节的最后一段: http : //beej.us/guide/bgnet/output/html/singlepage/bgnet.html

As a client, the application needs to be aware of the server port to which it needs to connect, and as a server, the application needs to be aware of the server port to which it needs to listen.作为客户端,应用程序需要知道它需要连接到的服务器端口,而作为服务器,应用程序需要知道它需要监听的服务器端口。 Therefore, the server needs to bind to an IP address and port so that the client can connect to it.因此,服务器需要绑定到一个 IP 地址和端口,以便客户端可以连接到它。

The client can simply create a socket and call connect() .客户端可以简单地创建一个套接字并调用connect() Binding to an available IP address and ephemeral port shall implicitly happen.绑定到可用的 IP 地址和临时端口应隐式发生。 But, yes, nothing prevents you from having the client bind to a particular IP address and port.但是,是的,没有什么可以阻止您将客户端绑定到特定的 IP 地址和端口。

In the case of UDP, despite there being no "connection" mechanism, servers still need to bind to IP addresses and ports to allow the clients to send data to them.在 UDP 的情况下,尽管没有“连接”机制,服务器仍然需要绑定到 IP 地址和端口,以允许客户端向它们发送数据。

On most systems, if you call sendto with a UDP socket that has not been bound, it implicitly binds it to INADDR_ANY and some currently unused port, so if that's what you want (which is the common case), there is no need for an explicit bind on the client.在大多数系统上,如果您使用尚未绑定的 UDP 套接字调用sendto ,它会将其隐式绑定到 INADDR_ANY 和一些当前未使用的端口,因此如果这是您想要的(这是常见情况),则不需要客户端上的显式绑定。

On some systems, trying to send with a unbound udp socket will give you a 'socket not bound' error, so if you want maximum portability, you should bind the client socket (to INADDR_ANY with port 0 to pick any currently unused port) before you try to send.在某些系统上,尝试使用未绑定的 udp 套接字发送会给您一个“套接字未绑定”错误,因此如果您想要最大的可移植性,您应该在之前绑定客户端套接字(到端口 0 的 INADDR_ANY 以选择任何当前未使用的端口)你尝试发送。

You either need to bind and then you can sendto(fd,data,destination) + recvfrom(fd), or you can just connect the socket and use send(fd,data) (without destination) and recv(fd).你要么需要绑定,然后你可以发送到(fd,数据,目的地)+recvfrom(fd),或者你可以只连接套接字并使用send(fd,data)(没有目的地)和recv(fd)。 The connect will do an implicit binding by using a more or less random port number and an local IP suitable for reaching the given target.连接将使用或多或少的随机端口号和适合到达给定目标的本地 IP 进行隐式绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM