Chat Room Comes Back With A New Face

Environment: VC6 /Win 2K/Win XP

 Index

 

Preface

This is not an update of my previous article. Although this is also a chat room program, it was written in a totally different way from its precedent. After my previous article was published, many people wrote to me for comments, which are highly appreciated. And many people wanted a "CSocket" version chat - room example too. So this article is an answer to their request. Since I am a physics student and have been busy playing with the detector simulation instead of Visual C++, I'd not even studied the CSocket mechanism until Matthew Millman 's excellent article came to the front page of Codeguru.

Inspired by the elegant implementation of his "NetMsg" program I decided to rewrite my chat room with CSocket too.

Another reason for me to do this piece of work was a realistic one: I'm on my way to graduation, and I want to begin my career as a programmer. And I hope my code would interest any potential employers. Please go see the statement below for details.

Screenshot



Click here for larger image

Basis

Before I dive into  the details of my code, I'd like to share some ideas about the programming with you guys, which I believe you'll benefited from as I have.

Java, C++, Basic, no matter what language you prefer to use, some principles are always to be held dear:

  • Think.  Dr. Stroustrup said in his "The C++ programming language", "always think more before coding". Always think the problem over and over, knowing what you are going to solve and the possible solution.
  • Keep the data process code and the GUI code separated always; GUI module is supposed to collect the data from some place( end user, I suppose?) and do the data presentation. Your program should always have one or more "information center" to hold these data(through variables) and manipulate them(through the methods or functions) and, at the right time , inform the GUI to update the presentation through interface. Take MFC SDI/MDI or dialog_base program as examples, for simple cases, the main dialog and the document class are the ok place to be "information center".  For large project, it might use the self-sufficient logic units as the building blocks, which have their own data manager classes, and have loosen relation with each other.

Advantage of using CSocket.

Now it's the time to talk my code. My previous program used the socket class from David Kruglinski's "Programming Visual C++". One of the disadvantages of that class was that it's extremely hard for the developer to send /receive more data types other than character stream.

But CSocket use the wonderful concepts of  CSocketFile and CArchive. If you have used CFile + CArchive before, you might realize how convenient it's to deal with different data. Its counterpart, CSocketFile, together with CArchive, can do the same thing for the network data streamming.

After some simple initialization work to link CSocket and CSocketFile and CArchive objects, we can focus our attention on the collecting and processing of data, almost all kinds of data*(I did have some problems dealing with the pointer, which is also a member for streaming in my program. Later I converted it into a DWORD so that it went to Archive happily),  then wrap them into package and send it to the CArchive object, MFC will take care of the rest Sending the package to the other computers with the predefined data order. So the whole programming became nothing more than the collection, assemble and distribution and parse of the data. See the diagram below.

Another advantage of CSocket is its virtual function overridden mechanism. Once the virtual functions of CSocekt like OnReceive, OnAccept have been overridden by the developer, we have set a network alert guard. For example, once there comes an incident message, OnReceive will alert and forward it to your corresponding handler). Similarly, once a request of connection comes, the OnAccept sends an alert to the handler too.

Diagram & explanations

Client:                                    



Click here for larger image

  • I believe the diagram would speak for itself. Still, I have to point out that the actual code would not strictly follow the building blocks shown above, as the design- programming is always an interactive activity which requires the continuous modification.

Server:



Click here for larger image

The design of the server is a litter different from the client.

7        Two CSocket derived classes are used. One is listening to the ports, once a request for connection is coming, a new clientsocket is created and its info is added to a linklist maintained by the main dialog (in my case, CChatServer).

7        I removed the burden of Sending/Receiving message from main dialog (CChatServer) to the client socket class. The main dialog is responsible for the forwarding and coordination of the messages between clients, in other words, a manipulation of the linklist

 

How to test this program

        Since only source file is provided, you need Visual C++ for compilation.

  •   Open the chat - server project, run it, click the "start" button, the IP of the current machine appears on the right;
  •   Open the chat client project, press "connection" button, change the IP address to the one shown on the server side, then ok. Next, press "Sign in" button, and begin to chat.
  •   Functionality of the buttons below are (from left to right): "Connection", "Sign in", "Sign out", "my icon"(N/A), "On leave"(N/A), "About".

           

            I used Visual C++ 6.0 / Window Xp/2K to do the test.

 

Features:

  • Rich text editing;
  • Persistence; your customized setting will be remembered and recalled next time.

 

Known bugs:

I did find some bugs when I operated server a lot (stop, start, stop, start) while the clients were still linking. For example, if the client logs off during the period while the server stops services, the other clients of cause couldn't get his log off info. Since in the normal circumstance the server is supposed not to be bothered after it's started, I did no bother to fix that part.

I tested on a single machine( Win XP installed) and also test in the LAN of my lab(Win2K installed ). I was carefully to avert the possible error. So I believe you guys will find many more bugs, just don't hesitated to let me know. Of course, if you can solve them on your own, that'd be better. J

Suggestions for update

  There is a lot of room for update.

  1. The icon button is a fake, no response to the users' input. It needs to be improved;
  2. The contact list box on the right is a fake, no icons - just names, and can't do anything. It needs to be improved for personal talk;
  3. The persistence is machine dependent instead of account dependent, which means different users get the same setting if he run this program on the same machine. It's not a mistake, but the account dependent setting is more "professional". Suggestion: use GetProfileIni/WriteProfileIni to do the persistence.
  4. Private conversations. People need to single out a certain guy for a private talk. Like MSN message; Matthem Millman did that with multi thread. But automation tech might be an alternative;
  5. File transfer;
  6. Pop3 mail reminder.

 I'll do these updates in the coming days, maybe after graduation.

Acknowledgement

I finished this project( maybe not yet J) within two weeks by myself, in my spare time at night. However, without the genius idea and code on codeguru, I couldn't make it. So many thanks should go to the people contributing their effort to this great website. Also, direct helps from the following guys are highly appreciated:

  • Matthem Millman   for his NetMsg and the clear presentation of  the usage of CSocket;
  • Kirk W. Stowell  for his cool CFlatcombobox  control;
  • Daniel G. Hyams for his great expansion dialog code, which goes into my code perfectly;
  • Bogdan Matasaru for his powerful property sheet wizard, which helped me to construct my server in a professional way.

Also many thanks go to the people emailed me, commented on my previous article, and answered my questions on the discussion board. I sincerely request you for the continuing supports for this one too.

Statement

As I mentioned at first, I am now looking for a proper job as a programmer. I like to use C/C++ and Java as my programming languages, my preferred IDE is Visual C++ studio and Forte from SUN. My computer skills are including but not limit to the programming ability. Web authoring and script writing are also my interests. I use Linux and IRIX from SGI doing my work as well. I also sometime consult on campus. I like to talk with people on all topics. At last, my loyalty and diligence is of no doubt because, this kind of work, is something I really fond of.

Work area includes any state within the U.S.A. Any offer or suggestion is quite welcomed. Please contact me through

  yihai@grant.phys.subr.edu, or swayhand@hotmail.com.

I'll send you my resume and ready to answer your questions.

Thank you all, and enjoy coding!

Download

Chat server source code (116 kb)
Chat client source code (342 kb)

History



Comments

  • Thank you very much !

    Posted by Legacy on 01/01/2004 12:00am

    Originally posted by: Rygor Babaebchenko

    Your programs are actually working ! Amazing ! You saved my own hide, thank you again ! 10 thousands years of happy life to you !

    Reply
  • How to make it support multi-threading?

    Posted by Legacy on 09/17/2003 12:00am

    Originally posted by: Jeff

    The code is working the and the comments are quite helpful.

    However, it seems that it is not using multi-threading, right?

    How to modify the code to run multi-threading?

    thx.

    Reply
  • !!!!!!!!!!!!not working in my xp

    Posted by Legacy on 08/26/2003 12:00am

    Originally posted by: sibilsalim

    not working...help me with some web based chat programs ..please

    Reply
  • Release mode crashes

    Posted by Legacy on 03/31/2003 12:00am

    Originally posted by: Randy Constan

    With all due respect to the work you have put into this, I must point out that this is the second version of the chatroom projects you've posted in which the release build crashes. In my opinion, DEBUG builds do so many things to "protect us" that I often wonder why I even bother using it. But if you are, as you say, looking for work as a VCC programmer, please take the friendly advice that surviving a release build is somthing your code should do BEFORE you share it.

    Reply
  • Thx

    Posted by Legacy on 03/14/2003 12:00am

    Originally posted by: Bastian

    Thank You ! it is helpfull but it doesn't work !!!
    I dont see clients, and I have lots of Asserts !
    Can You give me some working version ?
    Sorry for my english.

    Reply
  • Release mode working!!!

    Posted by Legacy on 12/13/2002 12:00am

    Originally posted by: Pappan

    Hi,

    I was testing the code in Release build.

    I changed the Project setting option for C/C++ from /O1 to /GZ. Then both Release and Debug is working fine.

    In VC++ editor, the default Project setting(C/C++ Project Options)for Compiler option is set to /O1. But in this mode some bugs normally arise only when switch to a Release build. We can use the /GZ option to enable run-time checks to catch these bugs in a Debug (/Od) build.

    Find more on MSDN.

    So i changed to /GZ option for Release build. Then it is working very fine.

    I really appreciate YiHai for this good work.

    Thank you very much
    :)
    Pappan

    Reply
  • Good Work..Error in Relase(Server.).. HostName not retrieving...

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

    Originally posted by: Pappan

    Hi,
    
    

    Error in Server Release mode, the HostName or the IP is not retrieving in. The server crashed.
    =====================================================
    CString CSockAddr::GetLocalIPAddress()
    {
    char szHostName[200];

    gethostname(szHostName,strlen(szHostName));

    ====> AfxMessageBox(szHostName);

    // in release szHostName return Null;
    .... ..... .......
    }
    =======================================================
    where will be the error occured....

    Debug mode working fine.

    Environment: VC6 /Win 2K

    Pl help me...
    thanks

    Reply
  • Excellent !

    Posted by Legacy on 08/24/2002 12:00am

    Originally posted by: MichelC

    Thank you very much for sharing your code. Your application is very educational = just what I need to develop mine.

    MichelC.

    Reply
  • Server doesn't work with VS7

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

    Originally posted by: Koaladan

    Compiling in Release with VS7 under win2k server creates an executable which crashes as soon as the "Start" button is pressed. Gives the message "The instruction at "0x00401ed2" referenced memory at "0x0000000c". The memory could not be "read"."

    Attempting to step through the "debug" executable causes VS7 to hang. ideas?

    Reply
  • So good!

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

    Originally posted by: kim duck hyun


    Thank you very much.

    I have reference much.

    The design is simple and the code distinct.

    But,source code of Properties,Send Msg and Delete

    not exist.

    Please Give me the source code of the control.

    Have a nice day!


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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Some of the statistics about the growth of enterprise mobility are surprising, not the least of which is the 4X increase in mobile malware from 2013 to 2014. Many employees today not only expect to be able to use their own personal devices at work, but will even contravene policies that restrict personal device use for work.  IT must find the right approach that balances the benefits of mobility with the risks involved.  This on-demand webcast offers steps toward developing a winning mobile strategy, and …

Most Popular Programming Stories

More for Developers

RSS Feeds