Question about bind( )

Started by
6 comments, last by certaintragedy 20 years, 5 months ago
In what cases would one bind a socket to anything other than INADDR_ANY? What other parameters are valid? Does anyone have any examples or know of any online? I''d just like to understand how this all works a bit better, and the help files don''t really explain any other valid values.
Advertisement
Basically, the only time I can think of that you specify something not INADDR_ANY is if the code is supposed to run on a multi-homed machined. That way you can specify which interface the socket will bind to.

Beej''s Tutorial shows how to use bind() without INADDR_ANY.
http://www.ecst.csuchico.edu/~beej/guide/net/html/
It''s kind of useful for load-sharing, routing, firewalling, etc. in situations where you may have overlapping subnets on two or more interfaces.

Generally, tho, you won''t need it for game development stuff.

"Sneftel is correct, if rather vulgar." --Flarelocke
You can also specify a multicast address when binding to theoretically have the kernel filter your incoming packets to just those from that multicast address. This doesn''t work so well under Win2000 in my experience though
No, a socket used to communicate with a multicast group still has a local address. bind()''ing to a multicast address shouldn''t work. In fact, if the socket didn''t have a local address, it''s very possible it wouldn''t be able to respond to or create IGMP packets properly.
You specify a local interface when joining a group via the ip_mreq structure. A lot of places including Stevens have documentation on being able to bind to the multicast group you wish to receive. Unfortunately, a lot of people seem to have trouble with this as it does not work a variety of systems. Maybe a difference in support between IGMPv2 and v3?
You usually want to bind to a specific NIC when making distributed game servers. There''s no reason why you would want to allow random people to try to connect to what should be an internal-only socket so you bind it on the internal net only.
-Mike
quote:Original post by jermz
You specify a local interface when joining a group via the ip_mreq structure. A lot of places including Stevens have documentation on being able to bind to the multicast group you wish to receive. Unfortunately, a lot of people seem to have trouble with this as it does not work a variety of systems. Maybe a difference in support between IGMPv2 and v3?


No, actually, I believe it''s a difference between posix.1 and posix.2, and IIRC Windows sockets were based on the posix.1 specification. (It''s one of the things that annoys me about Stevens'' book... he doesn''t specify what''s posix.2 specific.) Anyway, I think in posix.1 if there''s a call to bind(), a subsequent call to getsockname() is supposed to return the address used by bind(), which doesn''t work when bind()''ing to a multicast group, so bind() only works when specifying a local interface or ADDR_ANY.

This topic is closed to new replies.

Advertisement