An example class for a simple client derived from Client.
#include <iostream>
#define SUPER Client
: SUPER()
{
}
int iAdressFamily,
int iSocketType,
int iProtocol,
int iSecTimeOut )
: SUPER( uiPort, iAdressFamily, iSocketType, iProtocol, iSecTimeOut )
{
}
unsigned int uiPort,
int iAdressFamily,
int iSocketType,
int iProtocol,
int iSecTimeOut )
: SUPER( rstrHostname, uiPort, iAdressFamily, iSocketType, iProtocol, iSecTimeOut )
{
}
unsigned int uiPort,
int iAdressFamily,
int iSocketType,
int iProtocol,
unsigned int uiDataSize,
int iSecTimeOut )
: SUPER( rstrHostname, uiPort, iAdressFamily, iSocketType, iProtocol, uiDataSize, iSecTimeOut )
{
}
{
}
{
if ( pTcpMessage )
{
{
}
else
{
std::cout <<
"client get server-msg from server-socket[" << this->
m_MainSocket <<
"] with code [" << pTcpMessage->
getActionCode() <<
"]";
{
}
std::cout << std::endl;
}
SUPER::processServerMessage( pTcpMessage );
}
else
{
std::cout << "Err! No message!" << std::endl;
}
}
#undef SUPER
virtual void processServerMessage(tcp::Message *pTcpMessage)
std::string getHostName() const
Contains a data stream which can be sent and received by the Tcp classes.
Definition TcpMessage.h:168
int getActionCode() const
std::size_t getRawMsgSize() const
SOCKET m_MainSocket
Definition TcpMain.h:213
unsigned int m_uiServerPort
Definition TcpMain.h:156
#ifdef _MSC_VER
#ifdef _DEBUG
#include <crtdbg.h>
#define _CRTDBG_MAP_ALLOC
#endif
#endif
#ifdef _DEBUG_MEMORY_SPY
#include <MliForMain.h>
#endif
#include <iostream>
#if defined(_MSC_VER) || defined(__MINGW32__)
#include <conio.h>
#elif defined(__GNUC__)
#include <unistd.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
int _getche()
{
char ch = 0;
struct termios tOld;
fflush( stdout );
if ( tcgetattr( 0, & tOld ) < 0 )
{
perror("tcgetattr");
}
tOld.c_lflag &= ~ICANON;
tOld.c_lflag &= ~ECHO;
tOld.c_cc[VMIN] = 1;
tOld.c_cc[VTIME] = 0;
if ( tcsetattr( 0, TCSANOW, & tOld ) < 0 )
{
perror("tcsetattr ICANON");
}
if ( read( 0, & ch, 1 ) < 0 )
{
perror("read");
}
tOld.c_lflag |= ICANON;
tOld.c_lflag |= ECHO;
if ( tcsetattr( 0, TCSADRAIN, & tOld ) < 0 )
{
perror("tcsetattr ~ICANON");
}
return ch;
}
int _kbhit( void )
{
static int ch = 0,
fd = 0;
struct termios tNew,
tOld;
tcgetattr( fd, & tOld );
memcpy( & tNew, & tOld, sizeof( tNew ) );
tNew.c_lflag = tNew.c_lflag & ( ! ICANON );
tNew.c_cc[VMIN] = 0;
tNew.c_cc[VTIME] = 1;
tcsetattr( fd, TCSANOW, & tOld );
if ( ch != -1 ) ungetc( ch, stdin );
return ( ( ch != -1 ) ? 1 : 0 );
}
#endif
int main( int argc, char** argv )
{
#ifdef _MSC_VER
#ifdef _DEBUG
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
#endif
#ifdef _DEBUG_MEMORY_SPY
MemSpy::activateMemoryMonitoring();
#endif
int t_iErr = 0;
char t_Key;
std::string t_sClientOutput;
char * t_pBuffer = 0;
try
{
t_pClient =
new MyTestClient(
"LOCALHOST", 12345, PF_INET, SOCK_STREAM, 0, 150, 5 );
}
{
std::cout <<
"new MyTestClient Err: " << ERR.
error() << std::endl;
std::cout << "press key"; t_Key = _getche();
}
std::cout << "client started!" << std::endl;
t_iErr = t_pClient->connectServer();
if ( ! t_iErr )
{
std::string t_sIPAdress;
unsigned int t_uiPort(0);
t_iErr = t_pClient->getPeerName( t_sIPAdress, t_uiPort );
std::cout << "client ip:" << t_sIPAdress << " port:" << t_uiPort << std::endl;
do
{
std::cout << "input for server or 'q' for exit:" << std::endl;
std::cin >> t_sClientOutput;
if ( ! t_sClientOutput.empty() )
{
t_pBuffer = new char[ strlen( t_sClientOutput.c_str() ) + 1 ];
strcpy(t_pBuffer, t_sClientOutput.c_str());
t_pBuffer[ strlen( t_sClientOutput.c_str() ) ] = '\x00';
t_iErr = t_pClient->send( t_pBuffer, strlen( t_sClientOutput.c_str() ) + 1 );
if ( t_iErr )
{
delete[] t_pBuffer;
}
}
} while ( ! t_iErr && t_sClientOutput != "q" );
}
else
{
std::cout << "connection Err: " << t_iErr << std::endl;
}
t_iErr = t_pClient->disconnectServer();
t_pClient->cancel();
delete t_pClient;
#ifdef _DEBUG_MEMORY_SPY
MemSpy::finishMemoryMonitoring( MemSpy::OUTPUT_TRACE | MemSpy::OUTPUT_CONSOLE );
#endif
std::cout << "press key"; t_Key = _getche();
return 0;
}
Header for ErrorNException
An example class for a simple client derived from Client.
Definition MyTestClient.h:87
This exception class has an integer attribute for an error number in addition to the message.
Definition EuException.h:73
int error() const
Definition EuException.h:276