什么时候一个套接字就足够了,什么时候需要创建更多?

发布于 2024-09-13 03:00:04 字数 513 浏览 10 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浸婚纱 2024-09-20 03:00:04

UDP 套接字可以在两种不同的模式下运行:

  • 默认未连接模式:接收发送到进程的端口/地址的所有数据报;您需要为每次发送指定目标地址。
  • 连接模式:仅接收从您连接的地址/端口发送的数据报;您不需要在每次发送时指定目标地址。

以下是对连接的 UDP 套接字的简短回顾。

编辑:

这是一个小型 python UDP 服务器,它接受来自任何客户端的数据包并将它们复制到第二个服务器。一切都是通过一个未连接的 UDP 套接字完成的。

#!/usr/bin/env python
import sys, socket, string

if len( sys.argv ) != 4:
        print "Usage: udptee <local-listen-port> <copy-host> <copy-port>"
        exit( 1 )

copy = ( sys.argv[2], int( sys.argv[3] ))

s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
#s.bind(( 'localhost', int( sys.argv[1] )))
s.bind(( '', int( sys.argv[1] )))

print "listening on ", s.getsockname()
print "copying to", copy

while True:
        line, addr = s.recvfrom( 1024 )
        print "received: ", line, " from ", addr
        s.sendto( line, addr ) # echo to source
        s.sendto( line, copy ) # copy to tee
        if string.strip( line ) == "exit": break

print "Goodbye and thanks for all the fish"
s.close()

在一个终端中运行它:

~$ ./udptee 9090 <IP-of-copy-server> 9999

然后在第二学期以服务器模式启动 netcat 。该服务器将接受数据报的副本:

# this used to be "nc -ul 127.0.0.1 9999" which only listened on loopback
~$ nc -ul 9999

在第三个学期启动 netcat 客户端以将内容发送到第一台服务器:

~$ nc -u <IP-of-tee-server> 9090

开始输入并看到两个服务器都会回显您输入的内容。

UDP sockets can operate in two different modes:

  • default not-connected mode: all datagrams sent to the port/address of your process are received; you need to specify destination address for each send you do.
  • connected mode: only the datagrams sent from the address/port you connected to are received; you don't need to specify destination address on each send.

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.

#!/usr/bin/env python
import sys, socket, string

if len( sys.argv ) != 4:
        print "Usage: udptee <local-listen-port> <copy-host> <copy-port>"
        exit( 1 )

copy = ( sys.argv[2], int( sys.argv[3] ))

s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
#s.bind(( 'localhost', int( sys.argv[1] )))
s.bind(( '', int( sys.argv[1] )))

print "listening on ", s.getsockname()
print "copying to", copy

while True:
        line, addr = s.recvfrom( 1024 )
        print "received: ", line, " from ", addr
        s.sendto( line, addr ) # echo to source
        s.sendto( line, copy ) # copy to tee
        if string.strip( line ) == "exit": break

print "Goodbye and thanks for all the fish"
s.close()

Run it in one terminal as:

~$ ./udptee 9090 <IP-of-copy-server> 9999

Then start netcat in server mode in second term. This one will accept copies of the datagrams:

# this used to be "nc -ul 127.0.0.1 9999" which only listened on loopback
~$ nc -ul 9999

Start netcat client in third term to send stuff to the first server:

~$ nc -u <IP-of-tee-server> 9090

Start typing and see both servers echo what you type.

辞取 2024-09-20 03:00:04

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文