simple internet question?

Sorry another two questions on socket programming.

1) I was wondering why does the server need to bind() but the client doesn't?
The server needs to bind so the client needs to know what port to send data to...
so if the client doesn't bind to a port how does the server know what port to send data to?

2) Why does getaddrinfo() create a linked lists of addrinfos instead of just one?
Dont you just need one addrinfo for a socket?

Thanks
1) Actually the bind() function is used to assign the local address of the computer to the specified socket (http://linux.die.net/man/2/bind), otherwise the socket will not be accessible to the local machine. The server binding has nothing to do with the client, it is purely to facilitate the server.

The port that the server uses should be known by the client in advance of the connection. Without knowing what port the server is using the client will not be able to connect to it.

2) I don't know why but perhaps it would be worth creating a connection and then exploring this linked list to see what treasure it holds :P

This might also be an interesting read for you (http://linux.die.net/man/3/getaddrinfo) though it doesn't appear to explain your question.

--------------------------

I'd recommend reading this article (http://www.linuxhowtos.org/C_C++/socket.htm). You could learn a lot from it :)
Last edited on
Thank You for answering

I think I understand question number 2 better after messing around with it, and the articles really helped thanks :)
I still don't get the bind thing though- why does the computer need to bind a server and not a client? Its confusing

Thanks again :)
The purpose of bind() is to associate a server socket with a specific network address. For instance if you have 3 network cards in your computer, then you have three physical network addresses. The bind() function can be used to tell the server to communicate over a specific network cards, or even all of the network cards.

So when I run my apache web-server, I can configure it to just one specific network card, or over several network cards.

And if you think about it, each network card represent a differetn network, so bind() is used to specify which networks the server is available to.
Q1:The client does need to use bind(), too...

But usually it should be done implicitly or by binding to a random port (specify 0 and the system takes the next free one [at least for winsock]).



Binding is used for the system to know from which address-service-combination a socket is sending and receieving etc... (That´s not the 100% truth but I think You get what i mean^^)



Q2: It is possible for an ip to be associated to multiple names and so on...
Last edited on
Thanks I understand it much better now
Just confirming, but the client binds too, but it does it automatically because it doesn't matter what port I bind too right?
right( if you dont need to mind - as it should be)
Topic archived. No new replies allowed.