//udpserversocket.h
// ##### typedef zint32 SOCKET;
typedef void (*CALLBACK_RECV)(void *pThis, SOCKET sock, char *pData, zint32 iSize, sockaddr_in addr, zint32 addrLen);
class CUDPServerSocket
: public ZBaseThread
{
public:
CUDPServerSocket();
~CUDPServerSocket();
void SetHostIP(std::string strIp);
void SetHostPort(zuint16 usPort);
void SetRecvCallBack(CALLBACK_RECV onRecv);
void SetObjPointer(void *pThis);
bool SendDataToDes(char * buf, zint32 bufLen, const sockaddr_in & toaddr);
bool Init();
void Close();
private:
virtual void OnThreadProc();
std::string m_strIp;
zuint16 m_usPort;
bool m_ssActive;
CALLBACK_RECV mfn_Recv;
void * m_pThis;
SOCKET m_serverSock;
sockaddr_in GetNetAddr();
char sendBuf[BUF_LEN];
char msgBuf[MAX_MSG_LEN];
};
以上仅是提供一个思路, 具体的实现看你自己了, 你也可以提出自己的server类, 关键看你的需求是什么, 要实现什么之前,最起码知道你想要什么吧.
class CUDPCLientSocket
: public ZBaseThread
{
public:
CUDPClientSocket();
~CUDPClientSocket();
void SetRemoteAddr(std::string strIp, zuint16 usPort);
void SetRecvCallBack(CALLBACK_RECV onRecv);
void SetObjPointer(void * pThis);
bool Init();
bool SendDataToServer(char * buf, zint32 bufLen);
bool SendDataToDes(char * buf, zint32 bufLen, sockaddr_in toaddr);
void Close();
private:
virtual void OnThreadProc();
std::string m_strIp;
zuint16 m_usPort;
bool m_csActive;
void * m_pThis;
sockaddr_in m_serverAddr;
CALLBACK_RECV mfn_Recv;
SOCKET m_clientSock;
sockaddr_in GetNetAddr();
};
这个类是连接到服务器的客户端的抽象
class UDPSender_c
{
public:
UdpSender_c();
~UdpSender_c();
bool SendDataToDes(char * buf, zint32 bufLen, const sockaddr_in & toaddr);
private:
SOCKET _sock;
char sendBuf[BUF_LEN];
};
阅读(1150) | 评论(0) | 转发(0) |