Implementation of CClient

本文介绍了一个名为CClient的客户端类的设计与实现细节,该类用于处理与服务器之间的消息发送和接收。文章深入探讨了客户端如何创建套接字、绑定本地地址、连接到指定服务器并进行数据交换的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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 ); }//

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值