Multicast server and client in Python

Source: Internet
Author: User

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 clients who have a slightly different code. The major change is made in setting SOCKET options on the socket transmitting and inserting ing data.

TheServerCode looks like this:
import socket
import time

Any = "0.0.0.0 ″
Senderport = 1501
Mcast_addr = "224.168.2.9 ″
Mcaster 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 correspondingClientCode 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 ″
Mcaster 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)
Failed t 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 ES 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 addressShocouldBe a valid multicast address, andNotA Unicast (localhost/inaddr_any/any valid IP ).

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.