关闭

利用boost:asio写的简单聊天服务器四

标签: 服务器boost源代码
563人阅读 评论(0) 收藏 举报

二、客户端源代码

1. ChatClient.cpp (客户端main)

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
#include <boost/thread.hpp>

using namespace boost::asio::ip;
using namespace std;


class CClient
{
public:
	CClient(boost::asio::io_service& ios, const string& addr, unsigned port, const string& argv1)
		: m_socket(ios)
	{
		boost::asio::ip::tcp::endpoint endp(boost::asio::ip::address_v4::from_string(addr), port);

		m_socket.async_connect(endp, boost::bind(&CClient::handleConnect, this,
			boost::asio::placeholders::error, argv1));
	}

	void Write(const string& msg)
	{
		//cout << __FUNCTION__ <<endl;
		m_socket.get_io_service().post(boost::bind(&CClient::doWrite, this, msg));
	}
	
	void Close()
	{	
		m_socket.get_io_service().post(boost::bind(&CClient::doClose, this));
	}
	
private:

	void handleConnect(const boost::system::error_code& err, const string& registerName)
	{
		//cout << "CClient::handleConnect()" <<endl;		
		if (!err)
		{
			boost::asio::async_write(m_socket,boost::asio::buffer(registerName), 
				boost::bind(&CClient::handleWrite, this, boost::asio::placeholders::error));
		}
		else
		{
			std::cout << "handleConnect Error: " << err.message() << "\n";
			doClose();
		}
	}

	void handleWrite(const boost::system::error_code& err)
	{
		//cout << "CClient::handleWrite()" << endl;
		clearBuf();
		if (!err)
		{
			m_socket.async_read_some(boost::asio::buffer(m_rspBuffer),
				boost::bind(&CClient::handleRead,this,	
					boost::asio::placeholders::error,
					boost::asio::placeholders::bytes_transferred));
		}
		else
		{
			doClose();
			std::cout << "handleWrite Error: " << err.message() << "\n";
		}
	}

	void handleRead(const boost::system::error_code& err, int size)
	{
		//cout << "CClient::handleRead()" <<endl;
		
		if (!err)
		{
			string data(m_rspBuffer.data(), size);			
			if(strcmp(data.c_str(),"test"))
			{
				std::cout << data << std::endl;
			}
			clearBuf();
			m_socket.async_read_some(boost::asio::buffer(m_rspBuffer),
				boost::bind(&CClient::handleRead,this,	
					boost::asio::placeholders::error,
					boost::asio::placeholders::bytes_transferred));
		}
		else
		{
			std::cout << "handleRead Error: " << err.message() << "\n";
			doClose();
		}
	}
	
	void clearBuf()
	{
		boost::lock_guard<boost::mutex> guard(m_mut);
		int size = m_rspBuffer.size();
		if(size != 0)
		{
			for(int i=0; i<size; i++)
			{
				m_rspBuffer[i] = '\0';
			}
		}
	}
	
	void doWrite(const string& msg)
	{
		//cout << "CClient::doWrite()" <<endl;
		boost::asio::async_write(m_socket,boost::asio::buffer(msg), 
				boost::bind(&CClient::handleDoWrite, this, boost::asio::placeholders::error));
	}
	
	void handleDoWrite(const boost::system::error_code& err)
	{
		//cout << "CClient::handleDoWrite()" <<endl;
		if(err)
		{
			std::cout << "handleDoWrite Error: " << err.message() << "\n";
			doClose();
		}
	}
	
	void doClose()
	{
		m_socket.close();
	}

private:
	boost::array<char, 8192> m_rspBuffer;
	tcp::socket m_socket;

	boost::mutex m_mut;
};

int main(int argc, char* argv[])
{
	try
	{
		if(2!=argc)
		{
			std::cerr << "Usage: ./chatClient user:reigster_name\n";
			return 1;
		}
	
		boost::asio::io_service ios;
		CClient client(ios, "10.241.37.35", 9494, argv[1]);
		boost::thread bThead(boost::bind(&boost::asio::io_service::run, &ios));
		string strData;
		while(getline(cin, strData))
		{
			if(!strcmp(strData.c_str(),"quit"))
				break;
			client.Write(strData);
		}
		client.Close();
		bThead.join();
	}
	catch (std::exception& e)
	{
		std::cerr << "Exception: " << e.what() << "\n";
	}
	return 0;
}


功能较为简单,也有很多问题,希望各位能多提宝贵意见和建议。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3726次
    • 积分:74
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档