Implementation of CClient

 void CClient::send_message( const std::string& strMsg )

 if( send( m_iFlIndCli, strMsg.c_str(), SZBUFF, 0 ) < 0 )
  _error( "Can not send message to connected server!" );

 if( !_receive_from_server() ) exit( 1 );
}//

bool CClient::_bind()
{
 if( bind( m_iFlIndCli, ( TSOCKADDR* )&m_addrCli, sizeof( m_addrCli ) ) < 0 )
 {
  _error( "Can't bind to the computer(client)!" );
  close( m_iFlIndCli );
  return false;
 }//

 return true;
}//
  
bool CClient:: _socket()
{
 if( ( m_iFlIndCli = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
 {
  _error( "Can't create a sock at this client!" );
  return false;
 }

 return true;
}//

void CClient::_write_log( const char* sz )
{
 try
 {
  long tmsp = time( NULL );
  ///ctime( &tmsp );
  char szFileName[ SZBUFF ];

  sprintf( szFileName, "%i.log", tmsp );
  ofstream of( szFileName );
 
  of<<sz<<endl<<endl;
  

  cout<<"A log file is created... ..."<<endl;

  of.close(); 
 }
 catch( exception& e )
 {
  _error( const_cast<char*>( e.what() ) );
 }
}//

bool CClient::_receive_from_server()
{
 char szRet[ SZBUFF ];
 if( recv( m_iFlIndCli, szRet, SZBUFF, 0  ) < 0 )
 {
  _error( "Can't receive data from the asked server!" );
  close( m_iFlIndCli );
  return false;
 }
 
 if( m_fpGuiOutput )
  m_fpGuiOutput( szRet );
 
 cout<<"==================================="<<endl
     <<"Receive from server"<<endl
     <<"==================================="<<endl
     <<szRet<<endl
     <<"==================================="<<endl;

 _write_log( szRet );

 return true;
}//

bool CClient::_connect()
{
 socklen_t iLen = sizeof( m_addrSer );

 ///if( connect( m_iFlIndCli, ( TSOCKADDR* )&m_addrCli, iLen ) < 0 )
 if( connect( m_iFlIndCli, ( TSOCKADDR* )&m_addrSer, iLen ) < 0 )
 {
  _error( "The client can't connect to the server!" );
  close( m_iFlIndCli );
  return false;
 }
 
 return true;

}//
 
void CClient::_init( const string& strIP, int iSerPort, int iCliPort )
{
 if( !_socket() ) exit( 1 );
 ///
 ///configure for target server
 ///
 memset( &m_addrSer, 0, sizeof( m_addrSer ) );
 m_addrSer.sin_family = AF_INET;
 inet_aton( strIP.c_str(), &m_addrSer.sin_addr );
 m_addrSer.sin_port = htons( iSerPort );
 ///
 ///configure for local client who sends msg to the server
 ///this is not necessary done, because the port could not be always fixed.
 ///
 memset( &m_addrCli, 0, sizeof( m_addrCli ) );
 m_addrCli.sin_family = AF_INET;
 m_addrCli.sin_port   = htons( iCliPort );
 m_addrCli.sin_addr.s_addr = htons( INADDR_ANY );

 if( !_bind() ) exit( 1 );
 if( !_connect() ) exit( 1 );
 ///if( !_receive_from_server() ) exit( 1 );

}//


CClient::CClient( const string& strIP, int iSerPort, int iCliPort, FPOUTPUT fpGuiOutput  )
:m_fpGuiOutput( fpGuiOutput ),
 m_iFlIndSer( 0 ),
 m_iFlIndCli( 0 )
{
 _init( strIP, iSerPort, iCliPort );
}//

CClient::~CClient(){ close( m_iFlIndCli ); }//

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

allen1981813

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值