boost官方文档中聊天程序实例讲解
数据包格式chat_message.hpp
<pre name="code" class="cpp"><h3>数据包chat_message.hpp</h3>
// chat_message.hpp
// ~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//实例源于http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/examples/cpp03_examples.html 文档中没有注释,自己在///开始看的时候也走了点弯路,本文对该源码写了详细的注释,希望能让后来者们少走些弯路。
//首先看看数据包的结构:
//数据包分为两个部分,首先是报头占4bytes,每一byte表示一个数字,也就是最多能表示的出9999,不足在前填0;
// 数据包头的数字表示数据部分的长度大小。
#ifndef CHAT_MESSAGE_HPP
#define CHAT_MESSAGE_HPP
#include <cstdio>
#include <cstdlib>
#include <cstring>
class chat_message
{
public:
enum { header_length = 4 };
enum { max_body_length = 512 };
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()//将报头的4字节字符串转换成数字
{
using namespace std; // For strncat and atoi.
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; // For sprintf and memcpy.
char header[header_length + 1] = "";
sprintf(header, "%4d", body_length_);
memcpy(data_, header, header_length);
}
private:
char data_[header_length + max_body_length];
size_t body_length_;
};
#endif // CHAT_MESSAGE_HPP
聊天客户端chat_client.cpp
///<h3>
</h3><h3>
<span style="white-space:pre-wrap">聊天客户端chat_client.cpp</span>
</h3>
<pre>
///
//
// chat_client.cpp
// ~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
//boost