0

Hi I have two interfaces eth0 (192.168.1.115) and wlan0 (192.168.0.1). I want to send a broadcast on all. This is my code. But it only send a broadcast in the eth0 and I don't know why?

import socket
import time
import json
import threading

class BroadCaster(object):

    def __init__(self, MESSAGE):
        super(BroadCaster, self).__init__()
        self.MESSAGE = json.dumps(MESSAGE)
        self.UDP_IP = '<broadcast>' 
        self.UDP_PORT = PUERTO_DIFUSION
        self.sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock1.bind(('',0))
        self.sock1.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        self.sock1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        self.sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock2.bind(('192.168.0.1',0))
        self.sock2.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        self.sock2.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)


    def run(self):
        def broadcast(self):
            while True:
                print '* * Enviando * *'
                self.sock1.sendto(self.MESSAGE, (self.UDP_IP, self.UDP_PORT))
                self.sock2.sendto(self.MESSAGE, ('192.168.0.255', self.UDP_PORT))
                time.sleep( TIEMPO_ANUNCIOS )
        # manejo de hilos
        thread = threading.Thread(target=broadcast(self))
        thread.setDaemon(True)
        thread.start()

data = {'Nombre': OBJETO_PYRO, 'IP': DIRECCION_WS+':'+str(PUERTO_WS), 'IP_2':DIRECCION_BLUETOOTH ,'Mensaje': MENSAJE}
caster = BroadCaster(data)
caster.run()

I cant see the broadcast with tcpdump(i think that it doesnt send nothing), But if i put

self.sock2.sendto(self.MESSAGE, ('192.168.0.2', self.UDP_PORT))

it works, '192.168.0.2' is the other pc thats it is trying to see the broadcast.

xXcoronaXx
  • 13
  • 1
  • 8
  • If you want to broadcast using all the interfaces, then you have to bind sockets on them first. If you don't bind sockets on them individually then by default only `0.0.0.0` will be used which will be the first interface. What subnet mask are you using or is configured on the network interfaces? I assume a `/24`. If you transmit a packet to `192.168.0.255` it should route via the `wlan0` and if you transmit a packet to `192.168.1.255` it should route via `eth0`. This will only work if you have bound sockets on each interface and then transmit packets using those sockets. – ρss Apr 23 '15 at 09:59
  • how i bind the socket ? if i use setsockopt(..., SOL_SOCKET, SO_BINDTODEVICE, "ethX") the app crash. – xXcoronaXx Apr 23 '15 at 10:24
  • 1
    First get list of all the interfaces then iterate on this list. for each interface bind a socket and then use them to transmit the broadcast packets. – ρss Apr 23 '15 at 10:34
  • This is weird.This doesn't work `self.sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock2.bind(('192.168.0.1',0)) ... self.sock2.sendto(self.MESSAGE, ('192.168.0.255', self.UDP_PORT))` This works `self.sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock2.bind(('192.168.0.1',0)) .... self.sock2.sendto(self.MESSAGE, ('192.168.0.2', self.UDP_PORT)) #my other pc's IP` – xXcoronaXx Apr 23 '15 at 11:32

0 Answers0