Binding to the same port using INADDR_ANY and a specific IP at the same time

A simple experiment in python (on Windows) shows that I can bind to the same address at the same time for both a wildcard address and a specific address:

import socket
import select

MY_PORT = 13337

sany = socket.socket()
sany.bind(('', MY_PORT))
sany.listen(0)

sloc = socket.socket()
sloc.bind(('127.0.0.1', MY_PORT))
sloc.listen(0)

socks = [sany, sloc]
ready, _, _ = select.select(socks, [], [])
print socks.index(ready[0])

Conceptually, they intersect in what they should cover. Continuing the experiment, connecting to ('127.0.0.1', 13337)from another invitation, it is indicated that a more specific "victory" socket is printed (i.e. 1). I see similar behavior on sockets SOCK_DGRAM.

My questions are as follows:

  • Is this behavior contractual (Winsock, Berkeley Sockets, etc.)?
  • How should this behave for multicast sockets?
  • How should this behave on * nix systems?
+4
1

, , Windows Server 2003 , , bind() :

SO_REUSEADDR SO_EXCLUSIVEADDRUSE

​​ Windows Server 2003. Microsoft Server . Windows Server 2003 . , , , . , SO_REUSEADDR, . , , . .

, Windows Server 2003 , , , .

table

...

, . , Windows Server 2003 , , , .

table

Windows :

, Windows XP , , , .

table

...

, SO_REUSEADDR , , "" , , , "" .

+3

Source: https://habr.com/ru/post/1615128/


All Articles