C++自定义封装socket操作业务类完整实例
本文实例讲述了C++自定义封装socket操作业务类。分享给大家供大家参考,具体如下:
Linux下C++封装socket操作的工具类(自己实现)
socketconnector.h
#ifndefSOCKETCONNECTOR_H #defineSOCKETCONNECTOR_H #include"global.h" usingnamespacestd; classSocketConnector { public: typedefenum{ ENormal, EOther, }SocketState; public: staticSocketConnector*getInstance(); inlineSocketStatestate(){returnm_state;} inlinevoidsetState(SocketState_state){m_state=_state;} inlineboolisConnected(){returnm_isConnected;} inlinevoidsetConnected(boolstate){m_isConnected=state;} voidstart(); inlinevoidsetServerIP(stringip){m_server_ip=ip;} inlinevoidsetServerPort(intport){m_server_port=port;} intconnect_sockfd(); intonSendMessage(string&message); private: SocketConnector(); voidonConnectToServer(string&ip,intport); staticvoid*onReportMessage(void*p); staticvoid*onReadMessage(void*p); staticvoid*onWriteMessage(void*p); private: SocketStatem_state; boolm_isConnected; intm_sockFd; stringm_server_ip; intm_server_port; pthread_tm_report_tid; pthread_tm_read_tid; pthread_tm_write_tid; }; #endif//SOCKETCONNECTOR_H
socketconnector.cpp
#include"global.h" #include"socketconnector.h" #include"cmessagecenter.h" #include"cmip_requestparser.h" #include"csettings.h" #include"datadef.h" #include"cstringutils.h" usingnamespacestd; staticSocketConnector*g_instance=NULL; /************************************************************************************************** *SingleInstance. ***************************************************************************************************/ SocketConnector*SocketConnector::getInstance() { if(g_instance==NULL) { g_instance=newSocketConnector(); } returng_instance; } /************************************************************************************************** *Consturoctor ***************************************************************************************************/ SocketConnector::SocketConnector() { m_isConnected=false; m_state=ENormal; } /************************************************************************************************** *ConnecttoServerByBlockingMethod. ***************************************************************************************************/ voidSocketConnector::onConnectToServer(string&ip,intport){ cout<<__FUNCTION__<<"connecting::["<getKuType().c_str()); intreport_interval=atoi(CSettings::getInstance()->getKuReportinterval().c_str()); stringposition=CSettings::getInstance()->getKuPosition(); stringlocal_ip=CSettings::getInstance()->getKuAgentip(); cout< state(); content=" "+CStringUtils::toString(state)+" "; content+=""+CStringUtils::toString(devType)+" "; content+=""+position+" "; content+=""+local_ip+" "; Responseresp(STATUS_REPORT_CMD,0,string(content)); CMessageCenter::getInstance()->addReply(resp); sleep(report_interval); } } /************************************************************************************************** *ReadMessagefromConnection. *ThenSendMessagetoMessageCenterQueue. ***************************************************************************************************/ void*SocketConnector::onReadMessage(void*p) { SocketConnector*connector=(SocketConnector*)(p); if(NULL==p) { cout<<__FUNCTION__<<"onSelectSocket()Error:param[connector]isNULL"<connect_sockfd(); fd_setfds; structtimevaltimeout={0,0}; constintBUFFER_LEN=4*1024; staticcharbuffer[BUFFER_LEN]={0}; while(true) { FD_ZERO(&fds); FD_SET(sockFd,&fds); intret=select(sockFd+1,&fds,NULL,NULL,&timeout); switch(ret){ case-1:/*Errorprocess*/ { perror("select()"); if(EBADF==errno) { close(sockFd); connector->setConnected(false); sleep(1); sockFd=connector->connect_sockfd(); continue; } if(EINTR==errno||ENOMEM==errno) { sleep(1); continue; } }break; case0: { //cout<<"select()timeout!"< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"< addRequest(req); } }break; } } } /************************************************************************************************** *WriteMessagetoConnection. *ThenSendMessagetoMessageCenterQueue. ***************************************************************************************************/ void*SocketConnector::onWriteMessage(void*p) { SocketConnector*connector=(SocketConnector*)(p); if(NULL==p) { cout<<__FUNCTION__<<"onSelectSocket()Error:param[connector]isNULL"< getReplyMsg(msg); stringdata=CMessageEncoder(msg).encode(); connector->onSendMessage(data); } } /************************************************************************************************** *SendMessageBySocket. ***************************************************************************************************/ intSocketConnector::onSendMessage(string&strSend) { if(atoi(CSettings::getInstance()->getDebugMode().c_str())==1) { cout<<__FUNCTION__<<"SendToCmdwifiData::"< (strSend.size()); intnTotal=nLen; inti=0; while(1) { intnTmp=send(sock,&pData[i],nTotal,0); if(nTmp<=0) { close(sock); return-1; } nTotal-=nTmp; i+=nTmp; if(nTotal<=0) { break; } } return0; }
希望本文所述对大家C++程序设计有所帮助。