DisplayRawSocket and DisplayLineSocket

2013年6月7日 由 Creater 留言 »

忽略TcpSocket 的input缓冲区,OnRawData() 回调函数可以用来代替 OnRead()方法.
DisplayRawSocket.h

#include <TcpSocket.h>
#include <ISocketHandler.h>

class DisplayRawSocket : public TcpSocket
{
public:
	DisplayRawSocket(ISocketHandler& );

	void OnRawData(const char *,size_t);
};

DisplayRawSocket.cpp

#include "DisplayRawSocket.h"

DisplayRawSocket::DisplayRawSocket(ISocketHandler& h) : TcpSocket(h)
{
}


void DisplayRawSocket::OnRawData(const char *buf,size_t len)
{
	printf("Read %d bytes:\n",len);
	for (size_t i = 0; i < len; i++)
	{
		printf("%c",isprint(buf[i]) ? buf[i] : '.');
	}
	printf("\n");
}

DisplayLineSocket.h

#include <TcpSocket.h>
#include <ISocketHandler.h>

class DisplayLineSocket : public TcpSocket
{
public:
	DisplayLineSocket(ISocketHandler& );

	void OnLine(const std::string& );
};

DisplayLineSocket.cpp

#include "DisplayLineSocket.h"

DisplayLineSocket::DisplayLineSocket(ISocketHandler& h) : TcpSocket(h)
{
	SetLineProtocol();
}


void DisplayLineSocket::OnLine(const std::string& line)
{
	printf("Incoming: %s\n",line.c_str());
}
广告位

发表评论

你必须 登陆 方可发表评论.