Why bind a socket to an address?
Before 'listen'ing on a socket I must first bind it to an address. Why? It seems to me that I shouldn't have to pick and should just listen to that port on all 开发者_如何转开发addresses. Even if I bind to 127.0.0.1 though, it seems I can still connect to that socket from a remote machine. What does binding to an address really do?
Binding to an address defines exactly which interface should have that server port open. binding to 0.0.0.0 is a special case when you want to listen to all IP addresses know to the interface. Binding specifically to 127.0.0.1 should make the server port visible only to code that can connect to the localhost (e.g. local processes).
精彩评论