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

Started by
2 comments, last by Interesting Dave 16 years, 1 month ago
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]
Advertisement
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.
Roger that, lets run like hell!
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.
enum Bool { True, False, FileNotFound };
And thats why university sucks compared to real experience :)
Roger that, lets run like hell!

This topic is closed to new replies.

Advertisement