 ~Sockets~
|
C++ Socket library tutorial
|
|
Other implementations of the DisplaySocket: DisplayRawSocket and DisplayLineSocket
To bypass the TcpSocket input buffer, the OnRawData() callback method can be
used instead of the OnRead() method.
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");
}
|
|
When using a line based protocol, the OnLine() callback method can be used
to receive data. The OnLine() method is called for each complete line
recevied by the TcpSocket.
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());
}
|
|
|