Instant Messenger Chat (the message portion)

I am working on an instant messenger chat (like facebook chat or AOL instant messenger), and am struggling with what my code will need in order to work with the client and server code. I am only responsible for the Message portion of the entire project, and when I try to look at examples and tutorials (like Boost), they mainly show client and server code. I know my code is missing a lot from the looks of it.

Ideally I would like for the user to be able to type a message, and have the "sender" and "receiver" appear when each message is received so they know who sent it.

So the two things I am struggling with are:
1. What I am missing and need to include in my code that will make each message the person writes to be an easily readable packet.

2. If I do need a GUI, what is the simplest way I could try to tackle this? I use QT and I've read that it is possible to make a simple one using that, although I am not sure how.

Thanks and let me know if I need to elaborate on anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef MESSAGE_
#define MESSAGE_

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

    //Pseudo code for client/server end
    //string host       = "localhost";
    //int port          = 9502;

    // username and password
    //string username   = "admin";
    //string password   = "password123";



class sendchatMessage {
private:
    string originator, recipient;
    string message;
    size_t body_length_;


public:

    enum { header_length = 4 };
    enum { max_body_length = 512 };

    sendchatMessage(const string orig, const string recip, string msg, size_t
 body = 0): originator(orig), recipient(recip), message(msg), 
body_length_(body) {

        cout<<msg.length()<<'\n';
        if(msg.length() <= max_body_length){
            //do what you gotta do
            cout<<"OK message size"<<'\n';
        }else{
            //create a new string
            //or throw an error.
            body_length_ = max_body_length;
            cout<<"error in message size"<<'\n';
        }

    }

   
    


};

class recievechatMessage{
private:
public:

    recievechatMessage(){

    }

    void display(){
       cout << "Print the message";
    }

};

int main()
{

    //static for now, should be dynamic
    string orig_nickname, recip_nickname;



    string answer;
    cout << "write something" << '\n';
    getline (cin, answer);
    cout << answer;
    int bodylength = answer.length();

    sendchatMessage chat1(orig_nickname, recip_nickname, answer, bodylength);



}

#endif //MESSAGE_ 

Last edited on
You may need to take a step back and read a basic tutorial about socket programming in C++. Or, maybe you could benefit from searching for C++ chat server examples. Below are links and descriptions to two examples.

https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=9818&lngWId=3
Client and server are in the same executable. Only one client is supported because accept is not called in a loop. Windows only.

http://www.vbforums.com/showthread.php?781947-C-Simple-Chat-Program-In-a-Console-Window-(Winsock-amp-Multithreading)
Client and server are separate. Multiple clients supported. Windows only. Requires c++11 standard thread library, so be sure to use a recent version of Visual Studio.
Topic archived. No new replies allowed.