TcpLib
Loading...
Searching...
No Matches
MyTestClient Class Reference

An example class for a simple client derived from Client. More...

#include <MyTestClient.h>

Inheritance diagram for MyTestClient:
Collaboration diagram for MyTestClient:

Public Member Functions

 MyTestClient (const std::string &rstrHostname, unsigned int uiPort, int iAdressFamily, int iSocketType, int iProtocol, int iSecTimeOut)
 
 MyTestClient (const std::string &rstrHostname, unsigned int uiPort, int iAdressFamily, int iSocketType, int iProtocol, unsigned int uiDataSize, int iSecTimeOut)
 
virtual ~MyTestClient ()
 
- Public Member Functions inherited from tcp::Client
 Client (const std::string &rsHostname, unsigned int uiPort, int iAdressFamily, int iSocketType, int iProtocol, int iSecTimeOut)
 
 Client (const std::string &rsHostname, unsigned int uiPort, int iAdressFamily, int iSocketType, int iProtocol, unsigned int uiDataSize, int iSecTimeOut)
 
virtual ~Client ()
 
virtual void cancel () override
 
virtual void setDataSize (unsigned int uiDataSize) override
 
int connectServer ()
 
virtual int disconnectServer ()
 
bool isServerConnected () const
 
virtual int send (char *pszRawMsg, unsigned int uiRawMsgSize, unsigned int uiMessageID=0, int iActionCode=0, const std::string &rsInfo="")
 
virtual int send (Message *pTcpMessage) override
 
int acceptServerMessage (Message *pTcpServerMessage)
 
int getSocketAdrFromString (SOCKADDR_IN *Socketadress, const std::string &rsHostnameOrIp) const
 
std::string getHostName () const
 
int getPeerName (std::string &rsIPAdress, unsigned int &ruiPort) const
 
ActionCode getWaitingServerMsgActionCode () const
 
- Public Member Functions inherited from tcp::TcpMain
 TcpMain (unsigned int uiPort, int iAdressFamily, int iSocketType, int iProtocol, int iSecTimeOut)
 
 TcpMain (unsigned int uiPort, int iAdressFamily, int iSocketType, int iProtocol, unsigned int uiDataSize, int iSecTimeOut)
 
virtual ~TcpMain ()
 
unsigned int getPortNum () const
 
unsigned int getDataSize () const
 
SOCKET getMainSocket () const
 
void setTimeOutSec (int iSecTimeOut)
 
int TimeOutSec ()
 
- Public Member Functions inherited from eut::RepeatingThread
 RepeatingThread ()
 
virtual ~RepeatingThread ()
 
virtual int start ()
 
bool testCancel ()
 
RepeatingThread::STATUS status () const
 
bool isInExclusiveAccess () const
 
void setExclusiveAccess ()
 
void unsetExclusiveAccess ()
 
int getBreakCounter ()
 
virtual void takeABreak ()
 
virtual void finishBreak ()
 
void waitForInput ()
 
void signalInput ()
 

Protected Member Functions

 MyTestClient ()
 
 MyTestClient (unsigned int, int, int, int, int)
 
virtual void processServerMessage (tcp::Message *pTcpMessage)
 
- Protected Member Functions inherited from tcp::Client
 Client ()
 
 Client (unsigned int, int, int, int, int)
 
virtual void work () override
 
virtual TransmittergenerateNewTransmitter ()
 
virtual ClientReceivergenerateNewReceiver ()
 
virtual int syncDataSize ()
 
- Protected Member Functions inherited from tcp::TcpMain
 TcpMain ()
 
void setWSACleanup () const
 
int startWinsock () const
 
- Protected Member Functions inherited from eut::RepeatingThread
void threadWork ()
 
void setStatus (STATUS eStatus)
 
STATUS getStatus ()
 
void toOrder (ORDER eOrder)
 
ORDER getOrder ()
 

Private Member Functions

 MyTestClient (const MyTestClient &)
 
MyTestClientoperator= (const MyTestClient &)
 

Additional Inherited Members

- Public Types inherited from eut::RepeatingThread
enum  EuTHREADERROR { EuTHREAD_ERR_NO_THREAD = -10301 , EuTHREAD_ERR_THREAD_ALREADY_WORKING = -10302 , EuTHREAD_ERR_THREAD_BREAK_COUNT_LESS_THAN_ZERO = -10303 , EuTHREAD_ERR_UNKNOWN = -10400 }
 Error range = -10301 to -10400 More...
 
enum  STATUS {
  eAT_THE_START , eWORKING , ePAUSING , eENDING ,
  eCOMPLETED
}
 
enum  ORDER { eUNDEFINED , eDO_WORK , eEXIT }
 
- Protected Attributes inherited from tcp::Client
ClientReceiverm_pReceiver
 
Transmitterm_pTransmitter
 
- Protected Attributes inherited from tcp::TcpMain
int m_iSecTimeOut
 
unsigned int m_uiServerPort
 
unsigned int m_uiDataSize
 
int m_iAdressFamily
 
int m_iSocketType
 
int m_iProtocol
 
SOCKET m_MainSocket
 

Detailed Description

An example class for a simple client derived from Client.

Only the method MyTestClient::processServerMessage(tcp::Message * pTcpMessage) was overwritten.

Implementation of MyTestClient: MyTestClient.cpp

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#include "MyTestClient.h"
#include <iostream>
//-----------------------------------------------------------------------------
#define SUPER Client
//-----------------------------------------------------------------------------
: SUPER()
{
}
//-----------------------------------------------------------------------------
MyTestClient::MyTestClient( unsigned int uiPort,
int iAdressFamily,
int iSocketType,
int iProtocol,
int iSecTimeOut )
: SUPER( uiPort, iAdressFamily, iSocketType, iProtocol, iSecTimeOut )
{
}
//-----------------------------------------------------------------------------
MyTestClient::MyTestClient( const std::string & rstrHostname,
unsigned int uiPort,
int iAdressFamily,
int iSocketType,
int iProtocol,
int iSecTimeOut )
: SUPER( rstrHostname, uiPort, iAdressFamily, iSocketType, iProtocol, iSecTimeOut )
{
}
//-----------------------------------------------------------------------------
MyTestClient::MyTestClient( const std::string & rstrHostname,
unsigned int uiPort,
int iAdressFamily,
int iSocketType,
int iProtocol,
unsigned int uiDataSize,
int iSecTimeOut )
: SUPER( rstrHostname, uiPort, iAdressFamily, iSocketType, iProtocol, uiDataSize, iSecTimeOut )
{
}
//-----------------------------------------------------------------------------
{
}
//-----------------------------------------------------------------------------
{
if ( pTcpMessage )
{
if ( pTcpMessage->getActionCode() < 0 )
{
std::cout << "client get error-msg from server [" << this->getHostName() << "] port[" << this->m_uiServerPort << "] with code [" << pTcpMessage->getActionCode() << "]" << std::endl;
}
else
{
std::cout << "client get server-msg from server-socket[" << this->m_MainSocket << "] with code [" << pTcpMessage->getActionCode() << "]";
if ( pTcpMessage->getRawMsgSize() )
{
std::cout << " - " << pTcpMessage->getRawMsgPt();
}
std::cout << std::endl;
}
SUPER::processServerMessage( pTcpMessage );
}
else
{
std::cout << "Err! No message!" << std::endl;
}
}
//-----------------------------------------------------------------------------
#undef SUPER
//-----------------------------------------------------------------------------
Header for MyTestClient
virtual void processServerMessage(tcp::Message *pTcpMessage)
virtual ~MyTestClient()
std::string getHostName() const
Contains a data stream which can be sent and received by the Tcp classes.
Definition TcpMessage.h:161
int getActionCode() const
std::size_t getRawMsgSize() const
char * getRawMsgPt()
SOCKET m_MainSocket
Definition TcpMain.h:206
unsigned int m_uiServerPort
Definition TcpMain.h:149

The MyTestClient class can be tried out as follows (the server must be started first!):

ClientMain.cpp

#ifdef _MSC_VER
#ifdef _DEBUG
// detect Memory-Leaks
#include <crtdbg.h>
#define _CRTDBG_MAP_ALLOC
#endif
#endif
#ifdef _DEBUG_MEMORY_SPY
#include <MliForMain.h>
#endif
#include "MyTestClient.h"
#include <EuException.h>
#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
// Display the memory leaks if any are present...
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
// Next line with the memory address from "{ } ({56} normal block at 0x00D01C78, 10 bytes long.)".
//_crtBreakAlloc = 1149;
#endif
#endif
#ifdef _DEBUG_MEMORY_SPY
MemSpy::activateMemoryMonitoring();
#endif
int t_iErr = 0;
MyTestClient * t_pClient( 0 );
char t_Key;
std::string t_sClientOutput;
char * t_pBuffer = 0;
try
{
// If servers on other computers use the IP address instead of "LOCALHOST
t_pClient = new MyTestClient( "LOCALHOST", 12345, PF_INET, SOCK_STREAM, 0, 150, 5 );
}
catch( eut::ErrorNException ERR )
{
std::cout << "new MyTestClient Err: " << ERR.error() << std::endl;
std::cout << "press key"; t_Key = _getche();
return ERR.error();
}
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';
// The method to send (inherited from Client), could be overwritten
// for more functionality
t_iErr = t_pClient->send( t_pBuffer, strlen( t_sClientOutput.c_str() ) + 1 );
// only delete if error
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
// Display memory leaks
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:80
This exception class has an integer attribute for an error number in addition to the message.
Definition EuException.h:58
int error() const
Definition EuException.h:261
Author
Helmut Jakoby

Constructor & Destructor Documentation

◆ MyTestClient() [1/5]

MyTestClient::MyTestClient ( )
protected

This constructor is not available.

◆ MyTestClient() [2/5]

MyTestClient::MyTestClient ( unsigned int ,
int ,
int ,
int ,
int  )
protected

This constructor is not available.

◆ MyTestClient() [3/5]

MyTestClient::MyTestClient ( const std::string & rstrHostname,
unsigned int uiPort,
int iAdressFamily,
int iSocketType,
int iProtocol,
int iSecTimeOut )

The constructor starts the client, but is not yet connected to the server from rstrHostname.

Parameters
[in]rstrHostnameThe host name or IP address (e.g. "LOCALHOST" or "192.168.2.102").
[in]uiPortThe port of the server.
[in]iAdressFamilyThe socket address family to instantiate a socket (usually AF_INET ).
[in]iSocketTypeThe socket type (usually SOCK_STREAM ).
[in]iProtocolThe socket protocol (usually 0).
[in]iSecTimeOutThe time until waiting for a connection or waiting for data is aborted.

WSA is started.

Exceptions
eut::ErrorNExceptionAn exception is thrown if an error occurs.

◆ MyTestClient() [4/5]

MyTestClient::MyTestClient ( const std::string & rstrHostname,
unsigned int uiPort,
int iAdressFamily,
int iSocketType,
int iProtocol,
unsigned int uiDataSize,
int iSecTimeOut )

The constructor starts the client, but is not yet connected to the server from rstrHostname.

Parameters
[in]rstrHostnameThe host name or IP address (e.g. "LOCALHOST" or "192.168.2.102").
[in]uiPortThe port of the server.
[in]iAdressFamilyThe socket address family to instantiate a socket (usually AF_INET ).
[in]iSocketTypeThe socket type (usually SOCK_STREAM ).
[in]iProtocolThe socket protocol (usually 0).
[in]uiDataSizeThe packet size.
[in]iSecTimeOutThe time until waiting for a connection or waiting for data is aborted.

WSA is started.

Exceptions
eut::ErrorNExceptionAn exception is thrown if an error occurs.

◆ ~MyTestClient()

virtual MyTestClient::~MyTestClient ( )
virtual

In the destructor the connection to the server is terminated if not already done.

◆ MyTestClient() [5/5]

MyTestClient::MyTestClient ( const MyTestClient & )
private

The copy constructor is not available.

Member Function Documentation

◆ operator=()

MyTestClient & MyTestClient::operator= ( const MyTestClient & )
private

The assignment operator is not available.

◆ processServerMessage()

virtual void MyTestClient::processServerMessage ( tcp::Message * pTcpMessage)
protectedvirtual

Outputs a message from the connected server via std::cout.

Parameters
[in]pTcpMessageThe message to be processed from the server.

Reimplemented from tcp::Client.


The documentation for this class was generated from the following file: