Winsock, do you need to bind port when connecting?

Started by
8 comments, last by hplus0603 19 years, 8 months ago
Hi, in the Winsock2 for games tutorial, it says to bind a port to a socket then connect, but on Johnnie's winsock tutorial it doesn't say to bind. What do i do? thanks
Advertisement
I thought binding was only necessary when creating a listening socket. Hmm..
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.
On Winsock, the connect function will do an implicit bind. (MSDN)

Gizz
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.
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.
In winsock, TCP requires a port to connect and bind. UDP does not require a port.

Kuphryn
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
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.
Ah I completely see now, thanks
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.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement