Creating a Client/Server Framework

Environment: VC6 MFC

In creating my own multiplayer game played over the Internet, I first had to create a client/server framework so that players could interact with each other. After I finished creating the game, I generalized this framework for others to use.

For those not familiar with how a client/server framework works, one server sits on a computer somewhere out on the Internet, waiting for clients to connect. When clients successfully connect and log on to this server, they send messages to other clients via the server. This framework may be used for various applications, such as a chat application, a multiplayer game, or any application where clients need to communicate with each other.

The program that I have created includes some added features that I believe most people running this would benefit from. From incorporating Chris Maunder and Warren Young's work, you have the option of the server minimizing to the system tray, so that it is out of the way. To fully benefit from this feature, you may want to create a shortcut to the server's executable, and have this shortcut minimize on startup. Also, by using Adam Clauss's inifile class, you have the option of forcing users to log on with a password. Note that if you use this option, you must create a procedure for people to create a new account and password because this is not included. If different versions of your program are not compatible with each other, you may force users to all upgrade to the latest version using this framework. Finally, you may easily set how many days between logons must pass before a user is deleted from the user file.

If you are going to use this as a basis for your app, I recommend that you create a new project for the client program; then copy and paste the code. This is so that you may choose the name of the application yourself, as well as whether it's dialog-based or not. However, you don't have to recreate the server; users will never actually see this program.

One last thing to note is that the computer that runs the server must accept TCP connections on the port number that you run your application on. Therefore, make sure this rule is created on your firewall if you are running one. Also, the default IP address of the client program is 127.0.0.1, which translates to the local computer. Therefore, to connect client programs to the server residing on different computers, you must specify the server's actual IP address (type ipconfig at the server computer's Command Prompt to find out its IP address).

Good luck, and I hope that I have provided you with a head start to creating your client/server application!

Downloads

Download demo project - 53 Kb



Comments

  • how do client wait for a server reply?

    Posted by ophirf on 11/08/2004 04:58am

    Hello if a client sends the server a command and then it waits for the server reply. how can the client know that a reply from the server was received without using the event OnReceive?. Thanks in advance

    Reply
  • User.ini format

    Posted by Legacy on 11/28/2002 12:00am

    Originally posted by: Matt Minnis

    What is the format for the user.ini?

    I can't seem to get the client to connect when trying a user name & password.

    This is a great example though.

    Thanks

    Reply
  • Sorry to be a wet squib...

    Posted by Legacy on 11/19/2002 12:00am

    Originally posted by: Darwen

    I see you've got a socket implementation derived from CSocket.

    Under high loads (e.g. in games like quake etc) CSocket can
    block almost indefinately on receives, and I can't see how you are dealing with this.

    CSocket is fine for intermittant access but for high
    packet transfer, I've found it to be insufficient.

    Also you often need to do synchoronous transfer of information in a client/server mechanism. I can't see how you're handling this - i.e. send & wait for response.

    CSocket also relies on the message loop so other messages can interfere with operation. I've found that doing a send/receive operation takes ~200ms longer after responding to a key down message than a mouse click.

    Also, have you tried the server with about 20 users ? I think you'll find it'll fall over.

    Believe me, I've had many a fun hour pulling my hair out using CSocket.

    I eventually had to write my own implementation using raw winsock calls and multithreading to get it to work reliably. Can't post the code though - sorry - it's at work.

    Also, the client/server stuff should have been put into a library for re-use. The project at the moment has to be stripped down to be used, whereas if you'd have put all the client/server specific code into a library there'd be no need.

    Pretty good attempt, but incomplete I'd say.

    Darwen.

    Reply
  • How do I put this on my web page

    Posted by Legacy on 11/18/2002 12:00am

    Originally posted by: cmpssp

    I have a free web page at tripod.com
    I set up a dir for the server exe and a dir for the client
    exe, can I launch the server exe from my web page and how ?
    same goes for the client do I have top make a page and what
    is the call from html to run an exe ? I can get the IP addres but don't know how to run it from my web page.
    I'm very new at client server apps and html. Any help would be great. thanks
    my page is at http://delfly_zero.tripod.com
    it's a chat page.
    Also what files do I need in each dir ? files for the client
    and what files in the server dir ?


    Reply
  • Can't build at ServerProgram

    Posted by Legacy on 11/17/2002 12:00am

    Originally posted by: zeuss

    Hello:

    When I try to build ServerProgram project, I cause an error message like this: error C2065: 'IDANI_CAPTION' : undeclared identifier
    What's the problem?

    Reply
  • Nice!

    Posted by Legacy on 11/15/2002 12:00am

    Originally posted by: Cloaca

    Hi Jeff,

    Nice article. I am looking forward to working with it and I will comment more then. I was looking for a way to get a card game working accross the internet and you have saved me lots of time in getting the network part started. :-)
    I'll let you know how it turns out.

    Thanks,
    Eric

    Reply
  • Debug mode error correction

    Posted by Legacy on 10/29/2002 12:00am

    Originally posted by: Hyuck Kee Lee

    Replace " log.Close();"
    with " if(log.m_pStream != NULL)log.Close();"
    in OnInitDialog() and StartServer() functions
    God Bless you ...

    Reply
  • Thks man...

    Posted by Legacy on 09/20/2002 12:00am

    Originally posted by: Pedro

    You are my hero :)
    
    I was looking for a good starting point for a multiplayer game and your code really helped a lot. Thanks.

    Reply
  • SERVER - "Close problem"

    Posted by Legacy on 09/05/2002 12:00am

    Originally posted by: Johnson

    Just insert the following function for WM_CLOSE:

    void CServerProgramDlg::OnClose()
    {
    OnExitServer();
    }

    Cya :o)!

    Reply
  • good

    Posted by Legacy on 09/04/2002 12:00am

    Originally posted by: Jack

    thanks a lot
    

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • It's hardly surprising that half of small businesses fail within the first 1-5 years. It's not easy to launch a new product, single-handedly manage everything from IT to accounting, fend off the competition, and grow a customer base – all at the same time – even with a great concept. Offering awesome customer service can make the difference between a startup that flies and a startup that dies. Read this white paper to learn nine ways customer support can help you beat the competition and grow your …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds