Boost ASIO network chat code Modification Learning

Source: Internet
Author: User

Simplify the design of ASIO chat code removal room

All connected clients are under the same room

Chat message using the boost-from-Sample header file

#pragma once#include <cstdio> #include <cstdlib> #include <cstring>class chat_message{public:enum { Header_length = 4};enum {max_body_length =};chat_message (): body_length_ (0) {}const char* data () Const{return Data_;} char* data () {return data_;} size_t length () Const{return header_length + body_length_;} Const char* Body () Const{return Data_ + header_length;} Char* body () {return data_ + header_length;} size_t body_length () Const{return body_length_;} void Body_length (size_t new_length) {body_length_ = New_length;if (Body_length_ > Max_body_length) body_length_ = Max_ Body_length;} BOOL Decode_header () {using namespace Std;char Header[header_length + 1] = ""; Strncat (header, Data_, header_length); body_ Length_ = Atoi (header); if (Body_length_ > Max_body_length) {body_length_ = 0;return false;} return true;} void Encode_header () {using namespace Std;char Header[header_length + 1] = ""; sprintf (header, "%4d", Static_cast<int > (body_length_)); memcpy (data_,header,header_length);} PrivAte:char data_[header_length + max_body_length];size_t body_length_;}; 

MyChat.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <boost/enable _shared_from_this.hpp> #include <boost/asio.hpp> #include <set> #include <iostream> #include " Chat_message.h "using namespace boost::asio::ip;const int defaultport = 8899;class Chat_session;typedef boost::shared_ ptr<chat_session> chat_session_ptr;class chat_session:p ublicboost::enable_shared_from_this<chat_session >{public:chat_session (boost::asio::io_service& Io_service, std::set<chat_session_ptr>& Sessionlist): socket_ (Io_service), Sessionlist_ (sessionlist) {}~chat_session () {std::cout << "~chat_session ()" << Std::endl; }tcp::socket& socket () {return socket_;} void Start () {Sessionlist_.insert (Shared_from_this ()); Boost::asio::async_read (Socket_,boost::asio::buffer (read_ Msg_.data (), chat_message::header_length), Boost::bind (&chat_session::handle_read_header, Shared_from_this (), Boost::asIO::p laceholders::error));} void Handle_read_header (const boost::system::error_code& error) {if (!error && read_msg_.decode_header ()) {Boost::asio::async_read (Socket_,boost::asio::buffer (Read_msg_.body (), Read_msg_.body_length ()), Boost::bind ( &chat_session::handle_read_body, Shared_from_this (), Boost::asio::p laceholders::error));} else{//occur error,delete from Listsessionlist_.erase (Shared_from_this ()); Socket_.shutdown (Tcp::socket::shutdown_ both); Socket_.close (); return;}} void Handle_read_body (const boost::system::error_code& error) {if (!error) {std::cout << read_msg_.data () << Std::endl;boost::asio::async_read (Socket_,boost::asio::buffer (Read_msg_.data (), Chat_message::header_ Length), Boost::bind (&chat_session::handle_read_header, Shared_from_this (), Boost::asio::p laceholders::error) );} else{//occur error,delete from Listsessionlist_.erase (Shared_from_this ()); Socket_.shutdown (Tcp::socket::shutdown_ both); Socket_.close (); return;}} Private:char recvbuf[5];tcp:: Socket socket_;chat_message read_msg_;std::set<chat_session_ptr>& sessionlist_;}; typedef boost::shared_ptr<chat_session> Chat_session_ptr;class Chat_server{public:chat_server (boost::asio:: io_service& io_service,const tcp::endpoint& Endpoint): Io_service_ (Io_service), Acceptor_ (Io_service, Endpoint) {start_accept ();} void Start_accept () {chat_session_ptr new_session (new Chat_session (Io_service_, Sessionlist_)); acceptor_.async_ Accept (New_session->socket (), Boost::bind (&chat_server::handle_accept, this, New_session,boost::asio:: Placeholders::error));} void Handle_accept (chat_session_ptr session,const boost::system::error_code& error) {if (!error) {session-> Start ();} Start_accept ();} private:boost::asio::io_service& Io_service_;tcp::acceptor acceptor_;std::set<chat_session_ptr> Sessionlist_;}; typedef boost::shared_ptr<chat_server> CHAT_SERVER_PTR;INT Main () {Boost::asio::io_service io_service;tcp:: Endpoint endpoint (TCP::V4 (), defaultport); chat_seRVer Chatserver (Io_service, endpoint); Io_service.run (); return 0;}

  

Boost ASIO network chat code Modification Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.