问题
I have a server and client communicating through multicast using a python script. When I put my scripts on the same computer, they behave appropriately; that is they can send and receive data between each other. The computer is running Windows 10.
When I put one script on the computer and one script on a raspberry pi, my computer doesn't receive any multicasts except for it's own sent messages. The raspberry pi receives everything.
Can anyone tell me why my computer isn't receiving the data from the raspberry pi? There probably is a setting on my computer I need to enable or maybe I need to configure something in my script?
The code I use is below:
import socket
import struct
MCAST_GRP = '224.1.1.1'
MCAST_PORT = 5007
def _send_socket(message):
# Create socket to send validation on shooting
send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
dev = "eth0" + "\0"
#sock2.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, dev)
send_socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
send_socket.sendto(message, (MCAST_GRP, MCAST_PORT))
send_socket.close()
def _recv_socket(recieve_socket):
data = recieve_socket.recv(10240)
return data
def run():
recieve_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
recieve_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
recieve_socket.bind(('', MCAST_PORT))
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
recieve_socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
while(True):
data= _recv_socket(recieve_socket)
if data:
_send_socket("Sending something back")
来源:https://stackoverflow.com/questions/38364835/python-multicast-not-working-between-two-different-computers