🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

binding

Started by
0 comments, last by Taile 22 years, 7 months ago
I dont understand what binding is. What is it? Why do you have to do it to server sockets and not client sockets? What is the difference between a socket and the sockaddr(_in) stuff?
Advertisement
Well, think about the steps a bit.

You create a socket with socket(). You pretty much set up rules for a new socket. Then you have to attach this description to your network stack, so you bind() it. Once you do so, you can then call listen() to put your socket to queue incoming info, then call accept() to receive.

For example, if you didn''t call bind() before listen(), then your server would bind to a random port. That''s no good since clients won''t know what port to call upon when they connect().

Usually, when you''re a client, you could care less about what port you use to make your connection. So, calling connect() will pick a random local, source port and connect to server you request.

I guess the best way to think of bind() is a way to say, "Hey, I want this port, make sure it''s free for my use and give it to me."

If you wanted to have your client use the same local source port, say 9999, all the time, then you would use bind() there too and do recv().

Think of connect as a shorthand way to make a socket when you don''t need a specific local port. Since just about 99.9% of the servers need a specific local port, you don''t see connect used there.

As for socket vs sockaddr_in...well, that''s the difference between the function call to create a file descriptor to a socket and the address structure to hold the information about a socket. I think you might have to be more specific?

R

This topic is closed to new replies.

Advertisement