Skip to content

Simple Chat Application using Boost Asio, based on Cpp.

License

Notifications You must be signed in to change notification settings

BaseMax/BoostAsioChat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BoostAsioChat

Simple Chat Application using Boost Asio, based on Cpp.

Structure

Server

typedef deque<message> messageQueue;

class participant {
    public:
        virtual ~participant() {}
        virtual void deliver(const message& messageItem) = 0;
};

typedef shared_ptr<participant> participantPointer;

class room {
    public:
        void join(participantPointer participant);
        void deliver(const message& messageItem);
        void leave(participantPointer participant);
    private:
        messageQueue messageRecents;
        enum { max = 200 };
        set<participantPointer> participants;
};

class session : public participant, public enable_shared_from_this<session> {
    public:
        session(tcp::socket socket, room& room) : socket(move(socket)), room_(room);
        void start();
        void deliver(const message& messageItem);
    private:
        void readHeader();
        void readBody();
        void write();
        tcp::socket socket;
        room& room_;
        message messageItem;
        messageQueue Messages;
};

class server {
    public:
        server(boost::asio::io_context& io_context, const tcp::endpoint& endpoint) : acceptor(io_context, endpoint);
    private:
        void do_accept();
        tcp::acceptor acceptor;
        room room_;
};

int main(int argc, char* argv[]);

Client

typedef deque<message> messageQueue;

class client {
    public:
        client(boost::asio::io_context& context, const tcp::resolver::results_type& endpoints) : context(context), socket(context);
        void write(const message& messageItem);
        void close();
    private:
        void connect(const tcp::resolver::results_type& endpoints);
        void readHeader();
        void readBody();
        void write();
        boost::asio::io_context& context;
        tcp::socket socket;
        message readMessage;
        messageQueue writeMessage;
};

int main(int argc, char* argv[]);

Compile

$ g++ client.cpp -lpthread -o client
$ g++ Server.cpp -lpthread -o server

Using

  1. Start the server
$ ./server 4000
  1. Login as a user
$ ./client localhost 4000
first user: you can type message here...
  1. Login as a new user
$ ./client localhost 4000
second user: you can type message here...

Similar projects :