 ~Sockets~
|
Tutorial de la C++ Socket library
|
|
Creando un ejemplo de socket que devuelve un 'echo' de la entrada, en pantalla
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");
}
}
|
|
|