Home  +  Forums  +  C++ and Sockets  +  C++ and SQL: MySQL, sqlite, ODBC  +  Miscellaneous Projects
Logo
~Sockets~
~New~
~Examples~
~Contact~

C++ Socket library tutorial

Other implementations of the DisplaySocket: DisplayRawSocket and DisplayLineSocket

Prev   |   Menu   |   Next

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());
}


Prev   |   Menu   |   Next

Valid HTML 4.01!

Validate
Page, code, and content Copyright (C) 2021 by Anders Hedström