Sign in to follow this  

TCP Connections question

Recommended Posts

If I'm not going to be listening on a specific socket do I need to bind it? I'm trying to set up multiple TCP sockets so can I just create 1 listen socket and an accept socket then assign the accept socket to the listen socket then assign the listen socket to one of my multiple TCP sockets?

Share this post

Link to post
Share on other sites
You need to bind a socket to give it a known port. Not doing this means the socket gets one from the system at random. There's no way of 'listening on all ports'. Even if there was, why would you want to? The analogy of the IP address as the house, and the port as the recipient is a good one to bear in mind here.

TCP creates another socket when you accept. You don't need to explicitly create it. So simply:

On the server:
1) Create your listening socket
2) Set it up (ioctlsocket etc) and Bind it. Then call listen on it.
3) Start networking loop - if there's an incoming connection...
4) ...Call accept( ) on it. THIS CREATES A NEW SOCKET when successful. Store these! Also store the address AND PORT of the newly connected client, you can use this for client identification (helps if you need to implement UDP later).
5) When you have client sockets, do the usual select and send/recv business.
6) Go to 3 until you want the service to stop.
7) Clean up and exit.

On the client:
1) Create a socket
2) Don't bother binding - let the system pick a port.
3) connect the socket to your server's address and port (that you bound to).
4) If successful start the networking loop.
5) If connected do the usual select and send/recv stuff.
6) Go to 4 until you want the client to disconnect.
7) Disconnect the client cleanly, clean up and exit.

There is actually a little more to it in terms of managing the socket pool as clients come and go, but that's something that can be approached later once the basics are done.

Share this post

Link to post
Share on other sites
Original post by kuphryn
TCP socket is bind to a port before listen

UDP no require bind to any port


However you'll find that you bind a UDP socket on the server to a given port in order to allow people to find it. Having to portscan to make a connection is not a great design feature.

Share this post

Link to post
Share on other sites
No - but TCP and UDP are separate protocols as far as ports are concerned - so you *can* have both a UDP and a TCP socket operating on port 80, for example.

If a socket is not bound, it is assigned an unused port by the system. Opening two unbound UDP sockets will assign them different port numbers. Usually you'll find they're sequential (so first might be port 2563, then 2564 etc).

If you're using UDP however, you don't need more than one socket - it's connectionless. If you DO choose to connect a UDP socket (you can make the call on it) it basically just allows you to call recv( ) rather than recvfrom( ).

Share this post

Link to post
Share on other sites

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