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

📄 myclientsocket.h

📁 这是另外一个基于CAsyncSocket类,客户端程序!
💻 H
字号:

#pragma once

#include "ErrInfo.h"

//===========================================================================
//
//	摨婜揑偵捠怣偡傞僜働僢僩僋儔僗乮僋儔僀傾儞僩愱梡乯
//
//	CSocket 偲摨條偵丄Send 傗 Receive 傪摨婜揑偵張棟偡傞偨傔偺僋儔僗丅
//	CSocket 偩偲偆傑偔庴怣姰椆傪専抦偱偒側偐偭偨傝偟偨偺偱丄撈帺偺僋儔僗偵
//	偟偰傒偨丅
//	愙懕帪丄憲怣帪丄庴怣帪偵偦傟偧傟僞僀儉傾僂僩偵傛傞張棟拞抐傪峴偆丅
//	傑偨丄僄儞僪儐乕僓乕側偳奜晹偐傜偺僉儍儞僙儖傕壜擻丅
//
//===========================================================================

class CMyClientSocket : public CAsyncSocket
{
public:
	CMyClientSocket();
	virtual ~CMyClientSocket();

	bool SyncConnect(LPCTSTR lpszHostAddress, UINT nHostPort);
	bool SyncSend(const void* lpBuf, int nDataLen, int nFlags = 0);
	bool SyncReceive();

	void SetCancel();
	bool IsCanceled() const;
	int GetReceivedBytes() const;
	void RetrieveRecvData(CByteArray& recvData) const;
	const CErrInfo* GetErrInfo() const;

protected:
	// CAsyncSocket 偺僆乕僶乕儔僀僪
	virtual void OnConnect(int nErrorCode);
	virtual void OnReceive(int nErrorCode);
	virtual void OnClose(int nErrorCode);
	virtual void OnSend(int nErrorCode);

	// 撪晹偱巊梡偡傞僋儔僗
	class CMyTimer
	{
	public:
		CMyTimer(DWORD dwTimeOut)
			: m_dwTimeOut(dwTimeOut)
			, m_dwTimeBegin(::timeGetTime())
			{ }
		void ResetTimer()
			{ m_dwTimeBegin = ::timeGetTime(); }
		DWORD TimeLeft() const
		{
			DWORD dwEllapsed = ::timeGetTime() - m_dwTimeBegin;
			return m_dwTimeOut > dwEllapsed ? m_dwTimeOut - dwEllapsed : 0;
		}
	private:
		DWORD m_dwTimeOut;
		DWORD m_dwTimeBegin;
	};

	// 撪晹偱巊梡偡傞娭悢
	int doAsyncSendBuff();
	bool doReceive(LPCTSTR pszDebug);
	bool onMessagePending(const CMyTimer* pTimer);

protected:
	HANDLE m_hEventCancel;
	CCriticalSection m_cs;

	bool m_bConnected;
	CMyTimer m_timerConnect;

	CByteArray m_bufSend;
	int m_nBytesSent;
	CMyTimer m_timerSend;

	CByteArray m_bufRecv;
	CMyTimer m_timerRecv;

	CErrInfo m_errInfo;
};

⌨️ 快捷键说明

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