什么时候一个套接字就足够了,什么时候需要创建更多?
UDP 的“无连接”方面让我陷入了困境...
如果我将 UDP 套接字设置为 INADDR_ANY,然后将其绑定到本地计算机上的端口 33445,则该计算机将接受来自各个客户端的传入连接。所有这些连接都将由该套接字提供服务,因为这不是 TCP,并且我无法生成新的子套接字来直接处理每个连接。我可以通过他们的最新消息回复任何、部分或全部这些已连接的客户。
因此,事情对我来说有点模糊……
我是否也可以随时向这些客户中的任何一个发送消息?或者我只能发送响应 recvfrom() 的消息?
另外,如果我希望该服务器(当它为客户端提供服务时)连接到另一台服务器并就其他内容进行对话,我认为我需要为此目的创建一个新的套接字?我不能只使用现有的服务器套接字并指定新的目标地址?
非常感谢这个美好的社区。
编辑:让我换句话说..似乎我只能使用绑定的套接字来响应之前在该套接字上到达我的客户端。要启动与新主机的对话,我不能简单地使用绑定的套接字来实现该目的吗?我必须创建一个新的套接字才能到达正在侦听的服务器,对吗?
The "connectionless" aspect of UDP has thrown me for a loop...
If I setup a UDP socket set to INADDR_ANY, then bind that to port 33445 on the local machine, the machine will accept incoming connections from various clients. All of these connections will be serviced by that one socket, since this is not TCP and I cannot spawning a new child sockets to handle each connection directly. I am able to reply to any, some, or all of these connected clients, from their latest message.
So where things get a little fuzzy for me is here...
Am I able to also send out messages at any time to any of these clients? or can I only send messages that in response of recvfrom() ?
Separately, if I wanted that server (while it is serving clients) to connect to another server and have a conversation about something else, I assume I will need to create a new socket for this purpose? I cannot just use the existing server socket and specify a new destination address?
Many thanks to this wonderful community.
Edit: Let me put it another way.. It seems to be that I can only use the bound socket for responding to clients that have previously reached me on that socket. To initiate a conversation with a new host, I cannot simply use the bound socket for that purpose? I must create a new socket in order to reach a server that is listening, correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UDP 套接字可以在两种不同的模式下运行:
以下是对连接的 UDP 套接字的简短回顾。
编辑:
这是一个小型 python UDP 服务器,它接受来自任何客户端的数据包并将它们复制到第二个服务器。一切都是通过一个未连接的 UDP 套接字完成的。
在一个终端中运行它:
然后在第二学期以服务器模式启动
netcat
。该服务器将接受数据报的副本:在第三个学期启动
netcat
客户端以将内容发送到第一台服务器:开始输入并看到两个服务器都会回显您输入的内容。
UDP sockets can operate in two different modes:
Here's a small review of connected UDP sockets.
Edit:
Here's a little python UDP server that accepts packets from any client and copies them to a second server. Everything is done with one not-connected UDP socket.
Run it in one terminal as:
Then start
netcat
in server mode in second term. This one will accept copies of the datagrams:Start
netcat
client in third term to send stuff to the first server:Start typing and see both servers echo what you type.
UDP 套接字未连接到远程主机或客户端,因此您所需要做的就是使用目标地址和已初始化的 UDP 套接字来使用 sendto() 。所以,是的,只要您正确设置了 UDP 套接字,您就可以随时使用 UDP 套接字发送消息。只需在您正在使用的 sockaddr 结构中设置接收地址即可。如果接收方在您发送消息的端口上绑定了 UDP 套接字,那么它将接收该消息。
关于第二个问题,这完全取决于与第二台服务器的对话是否使用不同的端口。如果它使用相同的端口,则无需创建另一个 UDP 套接字。您只需以某种方式将服务器 1 从其客户端获取的消息与从服务器 2 获取的消息分开。
我建议您看一下 Beej 的优秀指南第 5.8 和 6.3 章。
Beej 网络编程指南
UDP sockets aren't connected to a remote host or client, so all you need to do is to use sendto() using the destination address and the UDP socket you have initialized. So yes, you can send out messages using the UDP socket at any time given that you have set up the UDP socket correctly. Just set the receiving address in the sockaddr struct you are using. If the receiving part have a UDP socket bound on the port you are sending the message, then it will receive it.
On that 2nd question it all depends if the conversation with that 2nd server uses a different port. If it uses the same port then there is no need to create another UDP socket. You just have to separate the messages the server 1 gets from its clients from messages it gets from server 2 somehow.
Ill recommend taking a look at Beej's excellent guide chapter 5.8 and 6.3.
Beej's Guide to Network Programming