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

Tutorial de la C++ Socket library

Creando un ejemplo de socket que devuelve un 'echo' de la entrada, en pantalla

Prev   |   Menu   |   Next

Esta clase Socket solo tiene un método (besides the constructor). OnRead() es llamado por SocketHandler cuando los 'datos' estan disponibles en el buffer de entrada TcpSocket. En esta implementación, OnRead() leerá un bloque de datos del buffer de entrada, e imprimirá en la consola mediante printf().

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

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

	void OnRead();
};

DisplaySocket.cpp
#include "DisplaySocket.h"

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


#define RSIZE 1024

void DisplaySocket::OnRead()
{
	TcpSocket::OnRead();
	size_t n = ibuf.GetLength();
	if (n > 0)
	{
		char tmp[RSIZE]; // <--- tmp's here

		n = (n > RSIZE) ? RSIZE : n;
		ibuf.Read(tmp,n);
		printf("Read %d bytes:\n",n);
		for (size_t i = 0; i < n; i++)
		{
			printf("%c",isprint(tmp[i]) ? tmp[i] : '.');
		}
		printf("\n");
	}
}


Prev   |   Menu   |   Next

Valid HTML 4.01!

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