Skip to content
Simple Chat Application using Boost Asio, based on Cpp.
Branch: master
Clone or download
Latest commit 0e2abda Jun 8, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Initial commit Jun 8, 2019
README.md Update README.md Jun 8, 2019
client.cpp Update client.cpp Jun 8, 2019
message.hpp Update message.hpp Jun 8, 2019
server.cpp Update server.cpp Jun 8, 2019

README.md

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 :

You can’t perform that action at this time.