Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Chat client server modification

Viki
2012-06-13
2013-05-14
  • Viki
    Viki
    2012-06-13

    void deliver(const chat_message& msg)
    {
    bool write_in_progress = !write_msgs_.empty();
    write_msgs_.push_back(msg);
    if (true)
    {
    boost::asio::async_write(socket_,
    boost::asio::buffer(write_msgs_.front().data(),
    write_msgs_.front().length()),
    boost::bind(&chat_session::handle_write, shared_from_this(),
    boost::asio::placeholders::error));
    }
    }

    void getthread()
    {
        boost::asio::io_service io_service;
        char line[chat_message::max_body_length + 1];
        chat_room room_;
        chat_session_ptr new_session(new chat_session(io_service, room_));
        while (std::cin.getline(line, chat_message::max_body_length + 1))
        {
            using namespace std; // For strlen and memcpy.
            chat_message msg;
            msg.body_length(strlen(line));
            memcpy(msg.body(), line, msg.body_length());
            msg.encode_header();
            new_session->deliver(msg);
        }
    }
    int main(int argc, char* argv[])
    {
        try
        {
            if (argc < 2)
            {
                std::cerr << "Usage: chat_server <port> [<port> ...]\n";
                return 1;
            }
            boost::asio::io_service io_service;
            boost::thread t(boost::bind(getthread));
            chat_server_list servers;
            for (int i = 1; i < argc; ++i)
            {
                using namespace std; // For atoi.
                tcp::endpoint endpoint(tcp::v4(), atoi(argv[i]));
                chat_server_ptr server(new chat_server(io_service, endpoint));
                servers.push_back(server);
            }
    
            io_service.run();
            t.join();
        }
        catch (std::exception& e)
        {
            std::cerr << "Exception: " << e.what() << "\n";
        }
        return 0;
    }
    

    this is modified code of chat in Boost sample. I need to display from client in the server and msg from server in the client.
    i Can send from client to server but wen i write from server, client does not read the message send. How to solve it?