udp

来源:互联网 发布:淘宝宝贝竖图怎么弄 编辑:程序博客网 时间:2024/06/04 20:02

                    最近要使用udp通信,所以写了关于udp的类,方便使用。

 common.h

#pragma once#ifndef IS_NULLPTR#define IS_NULLPTR(ptr)(nullptr == (ptr))#endif#ifndef NOT_NULLPTR#define NOT_NULLPTR(ptr)(nullptr != (ptr))#endif#ifndef IS_TRUE#define IS_TRUE(b)(true == (b))#endif#ifndef NOT_TRUE#define NOT_TRUE(b)(true != (b))#endif#ifndef IS_FALSE#define IS_FALSE(b)(false == (b))#endif #ifndef NOT_FALSE#define NOT_FALSE(b)(false != (b))#endif#ifndef IS_ZERO#define IS_ZERO(val)(0 == (val))#endif#ifndef NOT_ZERO#define NOT_ZERO(val)(0 != (val))#endif#ifndef SafDelete#define SafDelete(ptr)if(NOT_NULLPTR(ptr)){ delete (ptr);(ptr) = nullptr; }#endif#ifndef SafDelete_A#define SafDelete_A(ptr)if(NOT_NULLPTR(ptr)){ delete[] (ptr); (ptr) = nullptr; }#endif#ifndef SafCloseHandle#define SafCloseHandle(ptr)if(NOT_NULLPTR(ptr)){ CloseHandle(ptr);(ptr) = nullptr; }#endif#ifndef VALID_HSOCKET#define VALID_HSOCKET(hSocket)(INVALID_SOCKET != (hSocket))//valid socket handle#endif#ifndef INVALID_HSOCKET#define INVALID_HSOCKET(hSocket)(INVALID_SOCKET == (hSocket))//invalid socket handle#endif#ifndef SafCloseSocket#define SafCloseSocket(hSocket)if(VALID_HSOCKET(hSocket)){closesocket(hSocket);(hSocket) = INVALID_SOCKET;}#endif

UDPClient.h

#pragma once#include <WinSock2.h>class UDPClient{public:UDPClient();void SetSockAddr(const char *destAddr, const int destPort);//返回实际传输出去的字符数,失败返回-1int SynSendData(const char *pBuf, const int iBufLen) const;~UDPClient();private:WSADATA m_wsaData;SOCKET m_clientSocket;sockaddr_in m_clientSockAddr;};

UDPClient.cpp

#include "UDPClient.h"#include <WS2tcpip.h>#pragma comment(lib,"ws2_32.lib")#define _WINSOCK_DEPRECATED_NO_WARNINGSUDPClient::UDPClient(){WSAStartup(MAKEWORD(2, 2), &m_wsaData);m_clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);}UDPClient::~UDPClient(){closesocket(m_clientSocket);WSACleanup();}void UDPClient::SetSockAddr(const char *destAddr, const int destPost){m_clientSockAddr.sin_family = AF_INET;m_clientSockAddr.sin_port = htons(destPost);inet_pton(AF_INET, destAddr, &m_clientSockAddr.sin_addr);}int UDPClient::SynSendData(const char * pBuf, const int iLen) const{return sendto(m_clientSocket, pBuf, iLen, 0, (SOCKADDR *)&m_clientSockAddr, sizeof(m_clientSockAddr));}

UDPServer.h

#pragma once#include <WinSock2.h>class UDPServer{public:UDPServer();bool Listen(const int port);// 返回接收到的字节数,失败,返回-1   阻塞函数,直到有数据int SynReceiveData(__out char* buff,int iByteLen);void CloseSocket();~UDPServer();private:WSADATA m_wsaData;SOCKET m_serverSocket;sockaddr_in m_serverSockAddr;};typedef void(* pOnMsgProc)(const char * pMsg,int iMsgLen);class UDPServerManager{public:UDPServerManager();~UDPServerManager();bool Start(const int port, pOnMsgProc pMsgProc);void Stop(DWORD dwExitWaitTime = 2000);private:static unsigned __stdcall _ThreadProc(void * pArg);void _Clear();private:UDPServer * m_pUdpServer;HANDLE      m_hThread;pOnMsgProc  m_pMsgProc;volatile bool m_bIsContinue;boolm_bIsStart;};

UDPServer.cpp

#include "common.h"#include "UDPServer.h"#include <new.h>#include <process.h>#pragma comment(lib,"ws2_32.lib")UDPServer::UDPServer(){WSAStartup(MAKEWORD(2, 2), &m_wsaData);}UDPServer::~UDPServer(){CloseSocket();WSACleanup();}bool UDPServer::Listen(const int port){int iRet;m_serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);if (INVALID_HSOCKET(m_serverSocket))return false;m_serverSockAddr.sin_family = AF_INET;m_serverSockAddr.sin_port = htons(port);m_serverSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);iRet = bind(m_serverSocket, (SOCKADDR*)&m_serverSockAddr, sizeof(m_serverSockAddr));DWORD dw = GetLastError();return iRet == 0 ? true : false;}int UDPServer::SynReceiveData(char* buff, int iBuffLen){int serverSockAddrSize = sizeof(m_serverSockAddr);return recvfrom(m_serverSocket, buff, iBuffLen, 0, (SOCKADDR *)&m_serverSockAddr, &serverSockAddrSize);}void UDPServer::CloseSocket(){SafCloseSocket(m_serverSocket);}//////////////////////////////////////////////////////////////////////////////////////////////////////////////UDPServerManager::UDPServerManager() :m_pUdpServer(nullptr),m_hThread(nullptr),m_pMsgProc(nullptr),m_bIsContinue(false),m_bIsStart(false){}UDPServerManager::~UDPServerManager(){Stop();}bool UDPServerManager::Start(const int port,pOnMsgProc pMsgProc){bool bRet = false;if (IS_TRUE(m_bIsStart))return true;do{if (IS_NULLPTR(pMsgProc))break;m_pMsgProc = pMsgProc;m_pUdpServer = new (std::nothrow)UDPServer();if (IS_NULLPTR(m_pUdpServer))break;bRet = m_pUdpServer->Listen(port);if (IS_FALSE(bRet))break;m_bIsContinue = true;m_hThread = (HANDLE)_beginthreadex(nullptr, 0, &_ThreadProc, this, 0, nullptr);if (IS_NULLPTR(m_hThread))break;m_bIsStart = true;return true;} while (0);m_bIsContinue = false;SafDelete(m_pUdpServer);return bRet;}void UDPServerManager::_Clear(){m_pMsgProc = nullptr;SafDelete(m_pUdpServer);SafCloseHandle(m_hThread);m_bIsStart = false;}void UDPServerManager::Stop(DWORD dwExitWaitTime){if (IS_FALSE(m_bIsStart))return;m_pUdpServer->CloseSocket();m_bIsContinue = false;if (WaitForSingleObject(m_hThread, dwExitWaitTime) != WAIT_OBJECT_0){TerminateThread(m_hThread, 1);}_Clear();}unsigned __stdcall UDPServerManager::_ThreadProc(void * pArg){UDPServerManager *pThis = (UDPServerManager*)pArg;if (IS_NULLPTR(pThis))return 0;char pBuf[4096] = { 0 };while (IS_TRUE(pThis->m_bIsContinue)){int iRet = pThis->m_pUdpServer->SynReceiveData(pBuf, 4096);if (iRet != -1){pThis->m_pMsgProc(pBuf, iRet);}}//MessageBox(nullptr, L"UDPServer线程退出", L"TEST", MB_OK);_endthreadex(0);return 0;}

用mfc写的test demo

例子下载地址:
http://download.csdn.net/download/huanongying131/10136737






原创粉丝点击