zoukankan      html  css  js  c++  java
  • Multicast server and client in Python

    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
    import 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
    import 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).

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

  • 相关阅读:
    3D游戏常用技巧Normal Mapping (法线贴图)原理解析——高级篇
    [工作积累] OpenGL ES3.0: glInvalidateFramebuffer
    引擎设计跟踪(九.14.2c) 最近一些小的更新
    引擎设计跟踪(九.14.2b) 骨骼动画基本完成
    最近在玩的游戏
    引擎设计跟踪(九.14.2a) 导出插件问题修复和 Tangent Space 裂缝修复
    [工作积累] Google/Amazon平台的各种坑
    [工作积累] NDK通过Java获取package name 和version
    [工作记录] Android OpenSL ES: references & AAC related
    [工作记录] Android OpenGL ES: non-square texture
  • 原文地址:https://www.cnblogs.com/dkblog/p/2185240.html
Copyright © 2011-2022 走看看