⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qsocket.h

📁 BCB下的高效开发包。包含大并发的网络库
💻 H
字号:
#ifndef QSocketH
#define QSocketH
//---------------------------------------------------------------------------
#include "../QNetBase/QNetBase.h"
#include <string>
//---------------------------------------------------------------------------
class QBaseSocket
{
        public:
                QBaseSocket();
                ~QBaseSocket();
                void Close();
                void SetBlock(bool Block)const;
                bool WaitIn(int MSec)const;//毫秒
                bool WaitOut(int MSec)const;//毫秒
                bool WaitInOut(int MSec)const;//毫秒
                QConn GetRemote()const;
                QConn GetLocal()const;
                void Reset(int SocketHandle);
                int GetHandle()const;
                void SetOutTime(int Ms);
                int GetOutTime()const;
        protected:
                int m_Handle;
                int m_OutTime;
                static const int DEFAULT_OUTTIME = -1;
        private:
                QBaseSocket& operator= (const QBaseSocket&);
                QBaseSocket(const QBaseSocket&);
};
//传输Socket
//---------------------------------------------------------------------------
class QCustomSocket
	: public QBaseSocket
{
        public:
                QCustomSocket();
                bool Send(const std::string &Data,int *pSended)const;
                bool Send(const std::string &Data)const;
                bool SendPackage(const std::string &out_Data)const;//带头段
                bool Recv(std::string* pData, unsigned int GetLen = BUFFERMAX)const;
                bool RecvSeg(std::string* pData, const std::string &EndSign
    	            , unsigned int Max = 0xFFFFFFFF)const;//标志结尾的段
                bool RecvPackage(std::string* pData);
                void SetRecvSafeTimeOut(unsigned int Max = 0XFFFFFFFF);
                unsigned int GetRecvSafeTimeOut(void);
        protected:
                static const unsigned int BUFFERMAX   = 8 * 1024;
                static const unsigned int BUFFERTRANS = 4 * 1024;

                struct PackageSeg
                {
                        static const std::string HeadSign;
                };

        private:
                int m_RecvSafeTimeOut;//影响RecvPackage,RecvSeg;
};
//客户Socket
//---------------------------------------------------------------------------
class QTcpClientSocket
	: public QCustomSocket
{
        public:
                bool Connect(const std::string &IpAddr, unsigned int Port, bool Block=true);
                bool ConnectEx(const std::string &IpAddr, unsigned int Port, bool Block=true);
};
//服务器Socket
//---------------------------------------------------------------------------
class QTcpServerSocket
	: public QBaseSocket
{
        public:
                bool Bind(unsigned int Port);
                bool Accept(QCustomSocket* pCustomSocket)const;
};
//TCP服务器
//---------------------------------------------------------------------------
class QTcpServer
{
        public:
                QTcpServer();
                bool Start(unsigned int Port);
                void Stop();
                bool Active()const;
                QTcpServerSocket& GetServer();
        public:
                struct CSClient
                {
                        QTcpServer* lpMainServer;
                        QCustomSocket Client;
                };
        protected:
                virtual void ClientAccept(QCustomSocket&) = 0;//非const
                virtual bool ExecuteThread(void* lpThread,void* lpParam);
        private:
                friend int __stdcall _thListen (void* lpObject);
                friend int __stdcall _th_Ac (void* lpCSClient);
        private:
                bool m_Active;
                bool m_StopCmd;
                QTcpServerSocket m_Server;

};
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -