Jaxc
Jaxc

Reputation: 11

Difference in behaviour of socketbinding betwen Linux and Windows in Python

I'm making a python lib that I want to be able to use in both Linux and Windows. But I'm experiencing issues when I'm trying to bind a socket to listen to a broadcast. It seems like on linux I need to bind it to "255.255.255.255" but on windows I need to bind it to each interface manually.

I made this short test script that works on Linux but not on windows:

import socket

test_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
test_socket.bind(("255.255.255.255", 51337))
while True:
    print(test_socket.recv(1024))

On Linux the messages appears as expected, but on windows I get

OSError: [WinError 10049] The requested address is not valid in its context

If I instead change the broadcast address to the one currently used by the interface I want to listen to I get the messages as expected on windows, but on Linux I never receive anything. i.e:

import socket

test_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
test_socket.bind(("169.254.13.37", 51337))
while True:
    print(test_socket.recv(1024))

What am I doing wrong here, it feels like there should be a solution that doesn't involve coding different cases for windows and Linux?

Upvotes: 1

Views: 38

Answers (0)

Related Questions