Sign in to follow this  
Side Winder

What's the process of a user connecting to a server?

Recommended Posts

I'm looking to build a Chat server that users connect to and.. chat... Anyway, what's the order of things that has to happen in order for the users to connect to the server? For example, first of all, the server has to specify a port for the users to connect to, and also the IP address... What else needs to be done and in what order? edit: sorry, I should clarify. I'm looking at TCP here. [Edited by - Side Winder on March 3, 2008 7:24:54 AM]

Share this post


Link to post
Share on other sites
The server app creates a socket with a specific port number to 'listen on'
when it 'hears' a client trying to connect, it spawns a new tcp socket (on a new port) thats linked to that client (you probably want to make a new thread for it as well) and resumes listening on the orignal port.

a client creates a socket and tries to connect to the servers listen port (but ends up linking to the new one).

The server can't specify it's IP - the client will either have to know it already, or you'll need someway to 'broadcast' the server's IP.
For example if you have a static master-server that servers connect to, then a client can connect to the master server and ask for a list of available servers.

Share this post


Link to post
Share on other sites
Quote:
it spawns a new tcp socket (on a new port)


No, it retains the same port number on the server. There can be multiple sockets open with the same port number, as long as the remote endpoint (IP + port) is different for each. Thus, each new outgoing TCP connection to a given host will use a new port number on the initiating side.

In general, I think most of the questions in the original post are answered in the Forum FAQ, including some pseudo-code showing how a chat client and server will work.

Note that one thread per client is a terrible way of building server software, as it won't scale particularly well, and introduces all kinds of threaded race conditions in to your code. It's much better to use select() in a single thread, at least until you know better what it is you're doing, and can tune specifically for that.

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