Question

OK, so I cant seem to get a UDP port to bind to any interface, Ive tried wlan0/eth0/'0.0.0.0'/localhost etc, when using wlan0 as my interface i get no errors but when I run netstat -unl nothing is listed on the port. This should be simple, right?

def bind_udp_socket():
    if args.p < 1024 and os.getuid() != 0:
            print 'Cannot bind to privileged ports, run as root'
            exit(1)
    if args.v: print 'Binding to %s on UDP port %d' % (interface, args.p)
    try:
            s = socket.socket(socket.AF_PACKET, socket.SOCK_DGRAM)
    except socket.error, err:
            print 'failed to create socket:', err
            return 1
    try:
            #print type(interface) # debug
            s.bind((interface, args.p))
    except socket.error, err:
            print 'failed to bind to socket:', err
            return 1

    if args.v: print 'Socket bind complete'
    print 'Receiving data...\n'
    while True:
            data, addr = s.recvfrom(65535) # buffer size is 64 kilobytes
            if args.v: print data
    return 0

args.p is type int btw I ran the code on a different server (different OS) and same problem.

Was it helpful?

Solution

Your address family should be IPv4 (socket.AF_INET). As for the interface,

s.bind(("",UDP_PORT))

binds to all local interfaces on the receiving side.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top