目录
UDP
- TCP、UDP是经常成对听到通信协议,但许多常用协议如HTTP(HTTP3之前基于TCP,HTTP3基于QUIC基于UDP),SSH等都是基于TCP实现,原因其实很明显,就是TCP的可靠性。
- 最近在实现跨设备共享本地歌曲和控制的时候,发现了UDP的妙用。本文并不提供代码实现,只是聊聊这个技术概念,方便你做功能时技术选择。
单播、组播和广播
- 这三种通信方式是UDP的一大特色。TCP是全双工的一对一通信!我们在服务器上能监听一个端口后与多个客户端通信是Socket的功劳。也就是说,TCP是一对一通信,socket实现了一对多通信。
- 而UDP与TCP不同,它本身就支持一对一和一对多通信。
- 因而UDP分出了三种通信方式:
- 单播:一对一通信
- 广播:一对多通信。客户端监听一个固定的端口{port1},服务端只要向该网段的广播地址{addr1}和端口{port1}发送数据,该网段内所有监听这个端口{port1}的客户端都能收到信息。后面聊聊如何计算网段的广播地址。
- 组播(多播):一对多通信。客户端监听一个固定的端口{port1},并选择一个组播地址{addr1}加入。然后服务端向这个组播地址{addr1}和端口{prot1}发送数据,则所有加入这一组的客户端都能收到信息。相比手动循环给每一个客户端发送显然要快。
- 看起来广播和单播是两个极端,一个是一对一,一个是一对全部。而组播则是他们的取舍,能选择一组目标来发送数据。
- 但组播和广播仍然是有区别的,组播可以跨网段,而广播不行。
网段
- 我们得到分配的IP地址{ip}时,还会有一个子网掩码{mask},两者相与 {ip} & {mask} 即可得到网段,例如:
- IP地址 ip = 192.168.7.7
- 子网掩码 mask = 255.255.255.0
- {ip} & {mask} = 192.168.7.0
- 因此该路由分配下来的IP地址一般就是 192.168.7.x
- 注意子网掩码也可以是 255.255.0.0、255.255.240.0 等,看路由的设置
广播
- 实现广播的收发比较简单,客户端监听一个端口,服务端计算出目标客户端的网段的广播地址,然后向这个地址和端口发送数据即可。
- 注意服务端发送前需要启用广播(不同编程语言、不同库可能操作启用方式不同,请注意了解其使用方法 EnableBroadcast),然后发送的目标地址必须是广播地址(而且不能是 255.255.255.255,这个是受限地址),发送的目标端口必须是客户端监听的端口,因此客户端和服务器需要约定好通信端口。
- 关于广播地址的计算,比如:
- IP地址 ip = 192.168.7.7
- 子网掩码 mask = 255.255.255.0
- 网段 {ip} & {mask} = 192.168.7.0
- 将子网掩码转为二进制来看,11111111.11111111.11111111.00000000,按照其中0的位置,将网段的对应位置置为1,这里子网掩码为0的位置是最后8位,因此将网段转二进制后的最后8位置为1,再转回10进制即为 192.168.7.255
- 广播不能跨网段,比如:
- 客户端1:
- ip:192.168.7.7
- 子网掩码:255.255.255.0
- 网段 192.168.7.0
- 客户端2:
- ip:192.168.9.9
- 子网掩码:255.255.255.0
- 网段 192.168.9.0
- 你向 192.168.7.255 发送广播时,仅客户端1能收到信息
- 你向 192.168.9.255 发送广播时,仅客户端2能收到信息
- 你向 192.168.255.255 发送广播时,客户端1 和 客户端2 都不能收到信息!实践出真知,我试过!原本是以为用这个广播地址可以同时发给这两个客户端的,但实践结果是不行。
- 客户端1:
组播
- 客户端需要监听一个端口,并选择一个组播地址加入,而服务端需要向这个组播地址和端口发送数据即可。
- 组播地址是什么:
- 首先,IPv4地址是 4 * 8 = 32 个bit,4字节大小
- 它的地址被规定划分成如下分类:
- 其中,整个D类地址都被作为组播地址使用,而且组播地址还有细分:
- 如果只在局域网内使用,则取 239.0.0.0-239.255.255.255 中的任意一个都可以。
- 组播地址是自选的,客户端和服务器需要约定好选择的组播地址和端口,当发送端的目标地址和端口与客户端加入的组播地址和监听端口相同时才会收到信息。
技术选择
- 在局域网内网络没有广域网那么复杂,UDP表现挺不错的,另外TCP不支持组播和广播,而UDP支持,这在实现局域网内客户端互相发现、通信时相当有用,可以让客户端加入一个组,然后群发消息时发送组播即可。
- 由于每个客户端本身也有IP,可利用组播或广播互相发现对方,并在收到消息时提取发送方的IP,当需要一对一通信即可用这个IP直接通信。
- 但是我在实现时,运行在win11和安卓上的客户端通过热点WIFI连接,组播消息经常收不到,因此最后是使用广播实现的,如果实际使用请注意测试各种用户常用的网络环境。
参考资料
组播
- https://blog.csdn.net/weixin_41987016/article/details/132566896
Hast du Tipps für Anfänger bezüglich der besten Shops? Ich schaue mir bald die von dreadlocks haargummi #
pin-up casino: pin up aviator – pin-up casino giris
пин ап казахстан: пин ап кз – пин ап
pin up http://pinupkz.tech/# пин ап
pin up kz
pin up casino giris pin up giris pin up bet
пин ап казино онлайн https://pinupturkey.pro/# pin-up bonanza
пин ап 634
http://pinupru.site/# pin up казино
Прикольные анекдоты.
пинап казино: пин ап казино зеркало – pin up зеркало
В магазине сейфов предлагают cейфы 2 класс сейфы 2 класса
Thanks for the great explanation. Find more at https://medium.com/@christaleto8415/best-elbow-patch-blazers-26db414abe21
pin up казино: пин ап 634 – пин ап
pin up azerbaijan: pinup az – pin up 306
пинап кз http://pinupkz.tech/# пин ап кз
пин ап кз
пин ап казино: пин ап казино онлайн – пин ап 634
Наша юкос «Фотосайт по мебели чтобы кухни» занимается образованием а также реализацией лучшей кашеварной мебели. Я предлагаем широченный перечень продукции, который говорит наиболее современным стандартам и направленностям дизайна https://sufebey8kuhnishki.ru.
pin up casino: pin up 306 – pin up
pin up казино пин ап pin up зеркало
pin-up kazino: pin up azerbaijan – pin-up casino giris
Nunca está demás propiciar espacios donde fluyan ideas libremente entre compañeros trabajo!: eso genera innovación constante! Mira este sitio
blog review truyện tranh là nơi tôi thường xem những truyện tranh hay nhất thế giới
Лучшие идеи по изготовлению квадратных номеров, Как сделать стильные квадратные номера
пин ап казино онлайн http://pinupaz.bid/# pin up casino
pin up казино
https://pinupru.site/# pin up казино
This was a great article. Check out https://aqnailsspawestshore.com/ for more
El feedback de los clientes es oro. Estoy implementando cambios gracias a las sugerencias que he recibido a través de https://mssg.me/hjihp
http://pinupaz.bid/# pin up azerbaijan
пинап казино pin up пин ап казино зеркало
There’s nothing better than no deposit free spins, wouldn’t you agree? At PlayCroco casino we reward players that play online slots regularly. The more you play popular pokies, the better free bonuses you will unlock. The majority of free spins, no deposit bonus codes and match bonuses can be redeemed via your casino inbox (message center). Play Croco delivers on the best online pokies and Australia style slot machines, packed with thrilling gameplay options. Crocos can spin to win on 3 Reel, 5 Reel, 6 Reel bonus extravaganzas! Get scoring with best online casino reels, packed with bonus rounds, spectacular features, winning symbols and max bet, BIG WIN Progressive jackpot online pokies. 25 Free Spins at PlayCroco Casino 15 Free Spins at PlayCroco Casino Free spin bonuses are special offers that let players try out slot machine games without needing to spend any money upfront. Unlike other types of casino deals that give you extra cash to play with, free spins are only for use on slot games. With each free spin, you get to play a game round at no cost, and you could even win real money, just like if you were playing with your own cash. These bonuses are especially popular because they’re all about enjoying slot games without the worry of losing money.
https://preniumdirectory.com/listings12841947/slot-138-slot
Play on Betmaster? Note that you can enjoy ELK demo slots on your mobile device without the need for downloads. Just launch Clashofslots of your device, and play! Rummy 51 makes playing and winning easy and fair. With this easy-to-use platform, players can play fun and profitable rummy games. The rummy link is provided so you can participate directly and one win can change your life. This offer is only available to new players who made their FIRST deposit and selected this offer as a welcome bonus. The qualifying deposit must be made as a single transaction. All players must be 18+ years old and of legal age to gamble. Mustang Gold Megaways™ If you make a deposit while the previous deposit bonus is still being redeemed, the next deposit bonus will not be awarded. So, for example, if the First Deposit Bonus has not been redeemed, the Second Deposit Bonus will only be awarded after all the terms of the First Deposit Bonus have been met.
pin up casino guncel giris: pin up guncel giris – pin up guncel giris
pin-up casino giris: pin up aviator – pin up guncel giris
This was very enlightening. For more, visit https://medium.com/@altizermarjory521/best-charm-bracelets-for-women-5aac52b81431
pin up casino giris pin-up casino giris pin up bet
This was a great help. Check out https://camilobernard.medium.com/ for more
Фотофабрика кухонных гарнитуров в Санкт-петербурге – этто чемодан надежный партнер на организации кашеварных интерьеров. Мы работаем на разработке, производстве равно установке высококачественных кашеварных гарнитуров, тот или иной соединяют в себя язык, работоспособность равным образом долговечность. Наша миссия – вверить посетителям личные ответа, сформированные не без; учётом ихний пожеланий и необходимостей, чтоб любил шакша принялась приятным и спокойным местностью для бытия также творчества https://www.tivokya0kuhnishki.ru.
Наш магазин НашаМебель зовет широкий ассортимент кухонь, которые посодействуют создать уют и благоустроенность в вашем семье http://www.notahye4kuhnishki.ru.
Somebody essentially help to make significantly articles Id state This is the first time I frequented your web page and up to now I surprised with the research you made to make this actual post incredible Fantastic job
Профессиональный сервисный центр по ремонту бытовой техники с выездом на дом.
Мы предлагаем: сервисные центры по ремонту техники в волгограде
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
В магазине сейфов предлагают сейф 2 класса взломостойкости взломостойкий сейф 2 класса
пин ап пин ап официальный сайт пин ап официальный сайт
pin-up casino giris: pin up 306 – pin up azerbaijan
Ten Electric Standing Patio Heater Products That Can Improve Your
Life modern Free standing electric Fireplace
pin up aviator: pin-up casino – pin up aviator
Профессиональный сервисный центр ремонт сотовых телефонов ремонт телефонов с гарантией
Es impresionante ver cómo se ha utilizado el éxito para apoyar iniciativas de alto impacto Inclusión financiera en Latinoamérica
пин ап: pin up kz – пин ап
pinup az: pin-up kazino – pin up 306
pin-up casino giris [url=http://pinupturkey.pro/#]pin up casino guncel giris[/url] pin up casino
быстрый анонимный вывод из запоя на дому [url=www.vyvod-iz-zapoya-krasnodar15.ru]быстрый анонимный вывод из запоя на дому[/url] .
gomelsutochno.ru