双向UDP组播 [英] Bidirectional UDP Multicast

查看:275
本文介绍了双向UDP组播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为游戏服务器编写网络后端.

I am writing a network backend for a game server.

我计划使用多播,以便客户端可以通过多播套接字绑定到服务器.然后,服务器可以将游戏更新发送给该组的所有成员.

I planned on using multicast, so that clients could bind to the server via a multicast socket. The server could then send game updates to all members of the group.

但是,我想知道是否可以进行反向-客户端可以通过与多播套接字相同的端口将单播数据发送到服务器吗??

I'm wondering, however, if it's possible to do the reverse — can clients send unicast data to the server, over the same port as the multicast socket?

我已经基于 Java教程<(仅发送服务器->客户端),但是我无法使其适应双向通信.我在客户端上出现Address already in useNot a multicast address错误.

I've written a test program based off of the Java Tutorials (which only sends server —> client), but I was unable to adapt that to bidirectional communication. I'm getting Address already in use and Not a multicast address errors on the client.

我是否怀疑使用同一端口无法进行这种双向通信?我是否必须使用其他端口(一个用于多播[服务器->客户端],一个用于单播[客户端->服务器])?

Are my suspicions correct that such bidirectional communications are not possible using the same port? Must I use different ports (one for multicast [server —> clients], one for unicast [clients —> server])?

(我正在用Java进行此操作,但我对网络方面的东西是否有可能与实现方面的 .)

(I'm doing this in Java, but I'm more interested in the network-side-of-things is this possible vs. implementation-side how do I do this.)

推荐答案

是的,这是可能的.

假定IP地址为192.168.1.2的服务器希望将多播消息发送到224.1.2.3端口2222,并且接收单播驻留在端口1111上.在服务器端,将数据报套接字绑定到地址/端口0.0.0.0:1111 .在客户端,将数据报套接字绑定到0.0.0.0:2222,然后为多播组224.1.2.3注册该套接字.绑定其本地端口时,服务器可以选择指定192.168.1.2,但这不是必需的.客户端必须绑定到0.0.0.0,否则Linux系统将无法接收多播数据包.

Suppose a server with IP address 192.168.1.2 wants to send multicast messages to 224.1.2.3 port 2222 and receive unicast resposes back on port 1111. On the server side, bind a datagram socket to address/port 0.0.0.0:1111. On the client side, bind the datagram socket to 0.0.0.0:2222, then register the socket for multicast group 224.1.2.3. The server has the option to specify 192.168.1.2 when binding its local port, but is not required. The client must bind to 0.0.0.0, otherwise multicast packets can't be received on Linux systems.

服务器要发送时,它同时指定消息和目标的IP/端口.在这种情况下,服务器将使用其数据报套接字发送到224.1.2.3:2222,尽管您以后可以根据需要将其发送到其他地址/端口.生成的数据包的源IP/端口为192.168.1.2:1111,目标IP/端口为224.1.2.3:2222.

When the server wants to send, it specifies both the message and the IP/port of the destination. In this case, the server uses its datagram socket to send to 224.1.2.3:2222, although you can later send to a different address/port if you wish. The resulting packet has a source IP/port of 192.168.1.2:1111 and a destination IP/port of 224.1.2.3:2222.

为使客户端发送回服务器,它同时指定了消息和目标的IP/端口,本例中为192.168.1.2:1111.因此,生成的数据包的源IP/端口为{client_IP}:2222,目标端口为192.168.1.2:1111.服务器上只需要一个插槽,每个客户端上只需要一个插槽.

For the client to send back to the server, it specifies both the message and the IP/port of the destination, which in this case is 192.168.1.2:1111. So the resulting packet has a source IP/port of {client_IP}:2222 and a destination port of 192.168.1.2:1111. Only one socket on the server and one socket on each client are required.

这篇关于双向UDP组播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆