This gist is deprecated and will not be edited in the future. Consider visit ninedraft/python-udp repo. It will not be deleted, however.
Works for python 3.7 and 2.7 for Mac OS and Linux(kernel>=3.9) hosts. If you'ra using linux(kernel<3.9), then use socket.O_REUSEADDR
instead of socket.SO_REUSEPORT
to share (host, port)
between multiple clients and servers.
Tricks and traps:
- Socket portability issues: How do SO_REUSEADDR and SO_REUSEPORT differ?;
- Awesome "Socket Programming HOWTO";
How I do it not in local network?
I'm trying do this:
client.bind(("broadcast_station_ip", 37020))
And I'm getting this:
Traceback (most recent call last): File "client.py", line 15, in <module> client.bind(("broadcast_station_ip", 37020)) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 49] Can't assign requested address
For client.py I'm using macOS Big Sur 11.2.3 and for server.py I'm using Ubuntu 20.10.
It's work fine when:
client.bind(("", 37020))
and computers placed in local network.
If you have some idea or tips for solve this, please help me :)