几个UDP套接字,绑定到同一个端口?

几个UDP套接字,绑定到同一个端口?

问题描述:

首先 - 这不是如何绑定到另一个软件的端口"的问题.

first - it's not a question of "how to bind to port of another software".

现在,我有一个基于 TCP 的客户端-服务器应用程序,我想让它成为 UDP,但我错过了一些东西..

now, i have a client-server app, based on TCP and i'd like to make it UDP, but i'm missing something..

我对这个应用程序有 2 条规则(我已将其放入):
1)服务器"可以留在路由器后面,无需任何端口转发配置.
2)客户端"只能监听一个端口.

i have 2 rules (which i put) to this app:
1) the "server" can stay behind router without any port forwarding configuration.
2) the "client" can listen to only one port.

在 TCP 中我是这样做的:
1) 服务器在端口 X 上打开与客户端的初始连接.
2) 当客户端想要打开到服务器的通信通道时,它使用初始套接字向服务器请求一个新的套接字.
3) 服务器创建一个新的套接字并在端口 X 上连接到客户端.4) 客户端在端口 X 上接受这个新连接,现在客户端在这个新套接字上与服务器通信.

in TCP i do it like this:
1) the server opens initial connection to the client on port X.
2) when the client wants to open communication channel to the server it uses the initial socket to ask the server for a new one.
3) the server creates a new socket and connect to the client on port X. 4) the client accept this new connection on port X, and now the client talk with the server on this new socket.

这样我就可以在同一个端口上有多个连接.

this way i can have multiple connections on the same port.

在 UDP 中,我有一个小问题..
1) 服务器在端口 X 上向客户端发送初始连接 dgram.
2) 当客户端想要打开到服务器的通信通道时,它会向初始套接字的地址发送一个新套接字的请求.
3) 服务端收到消息,创建一个新的udp socket,并用它在端口X上向客户端发送数据.
4) 客户端收到新的 dgram,然后 ....?

in UDP, i have a little problem..
1) the server sends the initial connection dgram to the client on port X.
2) when the client wants to open communication channel to the server it sends request for a new socket to the initial socket's addr.
3) the server receives the message, creates a new udp socket, and use it to send data to the client on port X.
4) the client receives the new dgram, and ....?

基本上我现在想要发生的是接受"这种联系.意思是:创建一个新的 UDP 套接字,将它也绑定到端口 X 并仅从该特定传入套接字地址(ip,端口)接收数据.但我不能这样做,因为我无法将多个套接字绑定到同一个端口.
那么在一个端口上创建多个 udp 连接的方法是什么?(以网络方式,不仅仅是创建一个 dgrams 的环形缓冲区并发送到正确的套接字)

basically what i want to happen now is to "accept" this connection. meaning: to create a new UDP socket, to bind it also to port X and receive data only from that specific incoming socket addr (ip,port). but i cannot do that because i can't bind multiple socket to same port.
so what is the way to create multiple udp connections on one port? (in networking way, not just create a ring buffer of dgrams and send to the right socket)

谢谢:)

由于 UDP 是无连接协议,因此在步骤 4 中您检查 UDP 消息的内容并决定如何处理它.换句话说,消息的类型仅由其内容定义.

As UDP is connectionless protocol, on step 4 you check the contents of UDP message and decide how to handle it. In other words, the type of message is defined only by it's contents.

但是,我觉得你的整个设计有点错误.客户端 位于防火墙后面的情况更为常见(仅仅因为存在的客户端多于服务器).如果您需要将服务器置于防火墙之后,您只需配置防火墙以允许连接到一组端口.即使您只打开了一个端口,也不会阻止客户端多次并行连接到同一个服务器端口.

However, I have a feeling that your whole design is a bit wrong. It's much more common for the client to be behind firewall (just because there exist more clients, than servers). If you need to put the server behind firewall, you just configure the firewall to allow connections to the set of ports. Even when you have just one more port opened, nothing prevents the client from connecting to the same server port several times in parallel.