UDP广播的局域网中的计算机发现和放大器;服务器设置 [英] UDP broadcasting for LAN computer discovery & server setup

查看:224
本文介绍了UDP广播的局域网中的计算机发现和放大器;服务器设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个小的网络游戏原型,将使用UDP局域网进行播放。对于其他计算机在网络上发现,我一直在调查广播。不过,我仍然不确定有关UDP套接字设置/使用(网络新手)的一些细节。我发现一个好的图书馆比赛开始后使用,但在第一,运行游戏的所有计算机必须发现和一个必须选择一个服务器。所以我的问题有以下几点:


  • 可以单个UDP套接字用于侦听和发送广播?我是pretty肯定的答案,这是肯定的,但我想验证它。

  • 当使用UDP,你真的必须使用绑定()?据我了解,连接()是不需要的,也不是发送()/的recv(),因为他们是TCP(的sendto()/ recvfrom的()作为替代)。


解决方案

  1. 是的,你可以发送广播,单播发送和接收的数据包(广播或单播)从单一插座。这是制作回复发件人的工作十分有益。


  2. 不是每个插座需要使用绑定。如果你不这样做,一个端口将自动为您选择。但必须有人为了绑定pre-共享端口号为第一个数据包(可能是广播)要正确交付。第一个数据包包含源端口和IP地址;应答报文可以只使用此功能。


  3. 绑定两端固定的端口号,但​​确实使防火墙的配置更简单。


  4. 的setsockopt(SO_BROADCAST),否则你会得到错误尝试发送广播包。


I'm working on a small, networked game prototype which will be played on LANs using UDP. For the discovery of other computers on the network, I've been investigating broadcasting. However, I'm still unsure about a few details regarding UDP socket setup/usage (networking newbie). I found a good library to use after the game is started, but at first, all computers running the game must be discovered and one has to be chosen as a server. So my questions are the following:

  • Can a single UDP socket be used to listen for and send broadcasts? I'm pretty sure the answer to this is yes, but I wanted to verify it.
  • When using UDP, do you actually have to use bind()? As far as I understand, connect() is not required, nor is send()/recv(), since they are for TCP (sendto()/recvfrom() being the replacements).

解决方案

  1. Yes, you can send broadcasts, send unicasts and receive packets (either broadcast or unicast) all from a single socket. This is VERY useful for making "reply to sender" work.

  2. Not every socket needs to use bind. If you don't, a port will be chosen for you automatically. But someone has to bind a pre-shared port number in order for the first packet (possibly a broadcast) to be properly delivered. The first packet contains the source port and IP address; reply packets can just use this.

  3. Binding both ends to fixed port numbers does however make firewall configuration simpler.

  4. setsockopt(SO_BROADCAST), otherwise you'll get errors trying to send broadcast packets.

这篇关于UDP广播的局域网中的计算机发现和放大器;服务器设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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