|
| MyTestServer () |
|
| MyTestServer (unsigned int, int, int, int, int) |
|
| MyTestServer (unsigned int, int, int, int, unsigned int, int) |
|
virtual void | insertNewClient (Server::ServerReceiverTransmitter *pNewServerReceiverTransmitter) |
|
| Server () |
|
| Server (unsigned int, int, int, int, int) |
|
| Server (unsigned int, int, int, int, unsigned int, int) |
|
virtual void | work () override |
|
virtual void | insertNewClient (Server::ServerReceiverTransmitter *pNewServerReceiverTransmitter) |
|
Server::ServerReceiverTransmitter * | getClient (SOCKET Socket) |
|
virtual int | removeClient (SOCKET Socket) |
|
virtual Server::ServerReceiverTransmitter * | generateNewServerReceiverTransmitter (Server *pParent, SOCKET Socket, unsigned int iDataSize) const |
|
void | setClientMapReceiverValid (bool bValid) |
|
void | clearClientMap () |
|
unsigned long | getMaxClients () const |
|
virtual int | send (SOCKET ClientSocket, char *pszRawMsg, unsigned int uiRawMsgSize, unsigned int uiMessageID=0, int iActionCode=0, const std::string &rstrInfo="") |
|
virtual int | send (Message *pTcpMessage) override |
|
Protected Member Functions inherited from tcp::TcpMain |
| TcpMain () |
|
void | setWSACleanup () const |
|
int | startWinsock () const |
|
void | threadWork () |
|
void | setStatus (STATUS eStatus) |
|
STATUS | getStatus () |
|
void | toOrder (ORDER eOrder) |
|
ORDER | getOrder () |
|
An example class for a simple server derived from Server.
Messages from connected clients are output. Additionally, MyTestServer can send messages to all clients.
Implementation of MyTestServer:
MyTestServer.cpp
#include <iostream>
#define SUPER Server
: SUPER()
{
}
int iAdressFamily,
int iSocketType,
int iProtocol,
int iSecTimeOut )
: SUPER( uiPort, iAdressFamily, iSocketType, iProtocol, iSecTimeOut )
{
}
int iAdressFamily,
int iSocketType,
int iProtocol,
unsigned int uiDataSize,
int iSecTimeOut )
: SUPER( uiPort, iAdressFamily, iSocketType, iProtocol, uiDataSize, iSecTimeOut )
{
}
int iAdressFamily,
int iSocketType,
int iProtocol,
unsigned int uiDataSize,
unsigned long ulMaxClients,
int iSecTimeOut )
: SUPER( uiPort, iAdressFamily, iSocketType, iProtocol, uiDataSize, ulMaxClients, iSecTimeOut )
{
}
{
}
{
if ( t_pTcpServerMessage )
{
{
std::cout <<
"server get error-msg from client[" << t_pTcpServerMessage->
getSocket() <<
"] with code [" << t_pTcpServerMessage->
getActionCode() <<
"]" << std::endl;
}
else
{
std::cout <<
"server get client-msg from client[" << t_pTcpServerMessage->
getSocket() <<
"] with code [" << t_pTcpServerMessage->
getActionCode() <<
"]";
{
std::cout <<
" - " << t_pTcpServerMessage->
getRawMsgPt();
}
std::cout << std::endl;
}
}
else if ( t_pTcpServerAcceptMessage )
{
std::cout <<
"new client:" << t_pTcpServerAcceptMessage->
getSocket() << std::endl;
}
else if ( t_pTcpMessage )
{
std::cout << "Err! No message!" << std::endl;
}
if ( pTcpMessage ) SUPER::processClientMessage( pTcpMessage );
}
{
int t_iErr = 0;
std::map< SOCKET, ServerReceiverTransmitter * >::iterator m_ClientMapIterator;
char * t_pchBuffer = 0;
{
m_ClientMapIterator++ )
{
t_pchBuffer = new char[ strlen( strMsg.c_str() ) + 1 ];
strcpy(t_pchBuffer, strMsg.c_str());
t_pchBuffer[ strlen( strMsg.c_str() ) ] = '\x00';
t_pTcpMessage =
new tcp::Message( t_pchBuffer, strlen( strMsg.c_str() ) + 1, 0, 0,
"" );
t_iErr = (*m_ClientMapIterator).second->send( t_pTcpMessage );
}
}
return t_iErr;
}
{
int t_iErr(0);
std::string t_sIPAdress;
unsigned int t_uiPort(0);
SUPER::insertNewClient( pNewServerReceiverTransmitter );
t_iErr = pNewServerReceiverTransmitter->getPeerName( t_sIPAdress, t_uiPort );
std::cout << "new client ip:" << t_sIPAdress << " port:" << t_uiPort << " err:" << t_iErr << std::endl;
}
#undef SUPER
virtual void processClientMessage(tcp::AbstractMessage *pTcpMessage)
int sendToAllClients(std::string strMsg)
virtual void insertNewClient(Server::ServerReceiverTransmitter *pNewServerReceiverTransmitter)
Base class for messages which can be sent, received and processed by the Tcp-classes.
Definition TcpMessage.h:97
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
A special Tcp message, with which the ServerAcceptReceiver can register a new Client.
Definition TcpMessage.h:587
std::map< SOCKET, ServerReceiverTransmitter *, std::less< SOCKET > > m_ClientMap
Definition TcpServer.h:553
Is a special Tcp message for the Server. The socket is included so that the server knows to whom it s...
Definition TcpMessage.h:725
The MyTestServer class can be tried out as follows:
ServerMain.cpp
#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;
std::string t_sServerOutput;
char t_Key;
unsigned long t_ulMaxClients = 2;
try
{
t_pServer =
new MyTestServer( 12345, PF_INET, SOCK_STREAM, 0, 150, t_ulMaxClients, 5 );
}
{
std::cout <<
"new MyTestServer Err: " << ERR.
error() << std::endl;
std::cout << "press key";
t_Key = _getche();
}
std::cout << "Server started -> max clients="<< t_ulMaxClients << std::endl;
do
{
std::cout << "Input for all clients or 'q' for exit:" << std::endl;
std::cin >> t_sServerOutput;
t_iErr = t_pServer->sendToAllClients( t_sServerOutput );
} while ( ! t_iErr && t_sServerOutput != "q" );
t_pServer->cancel();
delete t_pServer;
#ifdef _DEBUG_MEMORY_SPY
MemSpy::finishMemoryMonitoring( MemSpy::OUTPUT_TRACE | MemSpy::OUTPUT_CONSOLE );
#endif
std::cout << "press key";
t_Key = _getche();
return t_iErr;
}
Header for ErrorNException
An example class for a simple server derived from Server.
Definition MyTestServer.h:85
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
- Copyright
- © 2010 Helmut Jakoby
- Author
- Helmut Jakoby