Archived

This topic is now archived and is closed to further replies.

Question about bind( )

This topic is 5145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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/

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites