Sign in to follow this  

Winsock, do you need to bind port when connecting?

This topic is 4867 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

Yep, you should only bind when listening. If you bind when connecting, you're forcing the system to use the port you specify, which is completely pointless (theres probably a valid reason, but I can't think of one now). If you don't bind when connecting, then the system will choose a port for you to use, and it should make sure that you don't end up with a port thats already in use.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Yep, you should only bind when listening. If you bind when connecting, you're forcing the system to use the port you specify, which is completely pointless (theres probably a valid reason, but I can't think of one now). If you don't bind when connecting, then the system will choose a port for you to use, and it should make sure that you don't end up with a port thats already in use.


Not completely, binding the socket allows you to use different functions, like send instead of sendto.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Evil Steve
Yep, you should only bind when listening. If you bind when connecting, you're forcing the system to use the port you specify, which is completely pointless (theres probably a valid reason, but I can't think of one now). If you don't bind when connecting, then the system will choose a port for you to use, and it should make sure that you don't end up with a port thats already in use.


so if i try to connect to an address, would winsock select the port that the server is listening on?


Quote:
Original post by Gizz
On Winsock, the connect function will do an implicit bind. (MSDN)

Gizz


So if i put a port in the structure " struct sockaddr*" would it bind that port for me?

thanks

Share this post


Link to post
Share on other sites
the port that you bind to has nothing to do with the port that you connect to. that is, the port on your local host has nothing to do with the port on the server host. in other words, you don't have to bind to port 21 in order to connect to port 21. you can bind to any port and connect to any port.

when you successfully connect(), the operating system reserves a port on the local machine for the socket connection. there is no 'bind' for client applications. there is a bind in the sense that a port is reserved, but you do not make a call to bind().

the server port, along with the server ip address, goes into the sockaddr structure. for example, if you wanted to connect to yahoo.com to get a web page, you would resolve yahoo.com's ip address (using gethostbyname()), then put the resolved address and port 80 into the sockaddr structure in network byte order, then connect() (no bind()). no where in this process do you really care what port is being used on the local host.

Share this post


Link to post
Share on other sites
Quote:

binding the socket allows you to use different functions, like send instead of sendto


This is incorrect. If you want to call send() instead of sendto() (which only makes sense for UDP sockets, not TCP sockets), then you have to call connect() on your socket, not bind().

You should bind() a socket if you use it for UDP; you should not bind() a socket if you use it for a TCP client, but you should bind() if you use it for a TCP server. You should not connect() a UDP socket, but you should connect() a TCP socket used for a client; you should not (and cannot) connect() a TCP socket used for a server.

When I say "should not" I'm aware that there are cases where you COULD, but in all useful cases, it's not a good idea.

Share this post


Link to post
Share on other sites

This topic is 4867 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this