博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
转-Multicast server and client in Python
阅读量:6489 次
发布时间:2019-06-24

本文共 4460 字,大约阅读时间需要 14 分钟。

  hot3.png

FROM:http://chaos.weblogs.us/archives/164

Python has complete support for sockets. Some of the APIs though have a different signature than the POSIX equivalents. It is therefore possible to write multicast servers and clients in Python. Multicasting is the ability to send data to select set of hosts. Broadcasting in a network is to send data to all the hosts, unicast is when data is transfered between two hosts (typical one-one communication). Multicasting is the ability to send data to a multicast address and the clients are provided the data. Now that an introduction to multicasting is complete, lets look at how to write a simple multicast server and a multicast client.
Multicast servers are written very similar to a standard unicast server, its the clients who have a slightly different code. The major change is made in setting socket options on the socket transmitting and receiving data.

The server code looks like this:

################################################################
import socket, time

ANY = "0.0.0.0"

SENDERPORT = 1501
MCAST_ADDR = "224.168.2.9"
MCAST_PORT = 1600

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)

#The sender is bound on (0.0.0.0:1501)
sock.bind((ANY,SENDERPORT))
#Tell the kernel that we want to multicast and that the data is sent to everyone (255 is the level of multicasting)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255)
while 1:
    time.sleep(10)
    #send the data "hello, world" to the multicast addr: port
    #Any subscribers to the multicast address will receive this data
    sock.sendto("Hello World", (MCAST_ADDR,MCAST_PORT));
################################################################
The TTL field of 255 indicates that the data is unrestricted in scope.

The corresponding client code to receive the data being transmitted by the sender will look like this:

################################################################
import socket, time

ANY = "0.0.0.0"

MCAST_ADDR = "224.168.2.9"
MCAST_PORT = 1600
#create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
#allow multiple sockets to use the same PORT number
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
#Bind to the port that we know will receive multicast data
sock.bind((ANY,MCAST_PORT))
#tell the kernel that we are a multicast socket
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255)
#Tell the kernel that we want to add ourselves to a multicast group
#The address for the multicast group is the third param
status = sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
    socket.inet_aton(MCAST_ADDR) + socket.inet_aton(ANY));

sock.setblocking(0)

ts = time.time()
while 1:
    try:
        data, addr = sock.recvfrom(1024)
    except socket.error, e:
        pass
    else:
        print "We got data!"
        print "FROM: ", addr
        print "DATA: ", data
################################################################
The import part of the client code is the setsockopt for multicast socket and to add the socket to a multicast group (socket.IP_ADD_MEMBERSHIP). Also note that the socket is bound to ANY:MCAST_PORT. The server sends data to the multicast address and on the client side, the socket that is bound to the multicast port subscribes to the multicast group and receives data on the MCAST_PORT. It is important the client code bind itself to the MCAST_PORT to receive data from the multicast address. Another important thing to note is the MCAST_ADDR. This address should be a valid multicast address, and not a unicast (localhost / INADDR_ANY / any valid IP).

TTL     Scope
----------------------------------------------------------------------
   0    Restricted to the same host. Won't be output by any interface.
   1    Restricted to the same subnet. Won't be forwarded by a router. <If not otherwise specified, multicast datagrams are sent with a default value of 1, to prevent them to be forwarded beyond the local network.
 <32         Restricted to the same site, organization or department.
 <64 Restricted to the same region.
<128 Restricted to the same continent.
<255 Unrestricted in scope. Global.

224.0.1.0到238.255.255.255为用户可用的组播地址,在全网范围内有效。其中232.0.0.0/8为SSM组地址,而其余则属于ASM组地址。

根据接收者对组播源处理方式的不同,组播模型分为以下两大类:
ASM模型:即任意源组播模型。在ASM模型中,任一发送者都可作为组播源向某组播组地址发送组播信息,接收者通过加入由该组播组地址标识的组播组以获得发往该组播组的组播信息。在ASM模型中,接收者无法预先知道组播源的位置,但可以在任意时间加入或离开组播组。
SSM模型:即指定信源组播模型。在现实生活中,用户可能只对某些组播源发送的组播信息感兴趣,而不愿接收其它源发送的信息。SSM模型为用户提供了一种能够在客户端指定组播源的传输服务。

转载于:https://my.oschina.net/kuafu/blog/37802

你可能感兴趣的文章
Asp.Net Core WebAPI入门整理(一)
查看>>
CSS Text
查看>>
Android 防内存泄露handler
查看>>
Redis整合Spring结合使用缓存实例
查看>>
【POJ 3292】 Semi-prime H-numbers
查看>>
时空的乐章
查看>>
Linux中Sed的用法
查看>>
腾讯 AlloyCrop 1.0 发布
查看>>
第三百四十节,Python分布式爬虫打造搜索引擎Scrapy精讲—css选择器
查看>>
js 字符串转换成数字的三种方法
查看>>
《JAVA与模式》之代理模式
查看>>
【dotnet跨平台】Asp.net 正在经历的变革
查看>>
Sql控制反转小尝试
查看>>
checkmysql.sh
查看>>
Android按键添加和处理的方案【转】
查看>>
如何让 Xcode 在读写上提速100倍?
查看>>
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password)
查看>>
SAP FICO 凭证导入接口 数据xml格式
查看>>
Jupyter Notebook快捷键
查看>>
概率运算中C(k,n)是怎么算的啊? 比如C(6,3)等于几?怎么来的.
查看>>