• PUDN用户
    了解作者
  • Visual C++
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 41
    下载次数
  • 2009-07-08 11:51
    上传日期
对UDP通讯进行了封装,运用回调函数进行数据发送前,接收后的数据处理。 特点:效率高,容易理解 是很好的学习代码
UDP_Socket.rar
  • UDP_Socket
  • CUDPSocket.h
    1.7KB
  • CUDPSocket.cpp
    5.4KB
内容介绍
#include "StdAfx.h" #include ".\filetransudp.h" //#include <stdio.h> #include "winsock2.h" #include <vector> //CUDPServerSocket #define BUF_LEN 65536 CUDPServerSocket::CUDPServerSocket(void) { WSADATA wsaData; int ret = WSAStartup(MAKEWORD(2,2), &wsaData); mfn_Recv = NULL; m_ssActive = false; } CUDPServerSocket::~CUDPServerSocket(void) { WSACleanup(); } void CUDPServerSocket::SetHostAddr(string strIp, USHORT usPort) { memset(&m_serverAddr, 0 , sizeof(m_serverAddr)); m_serverAddr.sin_family = AF_INET; m_serverAddr.sin_addr.s_addr = inet_addr(strIp.c_str()); m_serverAddr.sin_port = htons(usPort); } bool CUDPServerSocket::Init() { SOCKET recvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); sockaddr_in serverAddr = this->GetNetAddr(); int ret = bind(recvSocket, (SOCKADDR *)&serverAddr, sizeof(serverAddr)); this->m_serverSock = recvSocket; this->m_ssActive = true; HANDLE handle = CreateThread(NULL, 0, ServerThread, (LPVOID)this, 0, NULL); CloseHandle(handle); return true; } void CUDPServerSocket::Close() { if(this->m_ssActive) { this->m_ssActive = false; closesocket(this->m_serverSock); } } DWORD WINAPI CUDPServerSocket::ServerThread(LPVOID lpPara) { CUDPServerSocket * pSock = (CUDPServerSocket*)lpPara; SOCKET s = pSock->m_serverSock; char buf[BUF_LEN]; while(pSock->m_ssActive) { sockaddr_in senderAddr; int senderAddrSize = sizeof(senderAddr); memset(&senderAddr, 0, sizeof(senderAddr)); int ret = recvfrom(s, buf, BUF_LEN, 0, (SOCKADDR *)&senderAddr, &senderAddrSize); if(ret == SOCKET_ERROR) { //SOCKET ERROR,是否要关闭socket? } else { if(pSock->mfn_Recv != NULL) { pSock->mfn_Recv(pSock->m_pThis, s, buf, ret, senderAddr, senderAddrSize); } } } return 0; } bool CUDPServerSocket::SendDataToDes(char * buf, int bufLen, sockaddr_in toaddr) { int ret = sendto(this->m_serverSock, buf, bufLen, 0, (SOCKADDR*)&toaddr, sizeof(toaddr)); if(ret == SOCKET_ERROR) { //int re = WSAGetLastError(); return false; } return true; } //CUDPClientSocket CUDPClientSocket::CUDPClientSocket(void) { WSADATA wsaData; int ret = WSAStartup(MAKEWORD(2,2), &wsaData); mfn_Recv = NULL; m_csActive = false; } CUDPClientSocket::~CUDPClientSocket(void) { WSACleanup(); } bool CUDPClientSocket::Init() { SOCKET sendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); this->m_clientSock = sendSocket; this->m_csActive = true; HANDLE handle = CreateThread(NULL, 0, ClientThread, (LPVOID)this, 0, NULL); CloseHandle(handle); return true; } bool CUDPClientSocket::SendDataToServer(char * buf, int bufLen) { sockaddr_in serverAddr = this->GetNetAddr(); int ret = sendto(this->m_clientSock, buf, bufLen, 0, (SOCKADDR*)&serverAddr, sizeof(serverAddr)); if(ret == SOCKET_ERROR) { int re = WSAGetLastError(); return false; } return true; } bool CUDPClientSocket::SendDataToDes(char * buf, int bufLen, sockaddr_in toaddr) { int ret = sendto(this->m_clientSock, buf, bufLen, 0, (SOCKADDR*)&toaddr, sizeof(toaddr)); if(ret == SOCKET_ERROR) { //int re = WSAGetLastError(); return false; } return true; } void CUDPClientSocket::Close() { if(this->m_csActive) { this->m_csActive = false; closesocket(this->m_clientSock); } } void CUDPClientSocket::SetRemoteAddr(string strIp, USHORT usPort) { memset(&m_serverAddr, 0 , sizeof(m_serverAddr)); m_serverAddr.sin_family = AF_INET; m_serverAddr.sin_addr.s_addr = inet_addr(strIp.c_str()); m_serverAddr.sin_port = htons(usPort); } DWORD WINAPI CUDPClientSocket::ClientThread(LPVOID lpPara) { CUDPClientSocket * pSock = (CUDPClientSocket*)lpPara; SOCKET s = pSock->m_clientSock; char buf[BUF_LEN]; while(pSock->m_csActive) { sockaddr_in senderAddr; int senderAddrSize = sizeof(senderAddr); memset(&senderAddr, 0, sizeof(senderAddr)); int ret = recvfrom(s, buf, BUF_LEN, 0, (SOCKADDR *)&senderAddr, &senderAddrSize); if(ret == SOCKET_ERROR) { //没确认连接时会返回SOCKET_ERROR Sleep(100); } else { if(pSock->mfn_Recv != NULL) { pSock->mfn_Recv(pSock->m_pThis, s, buf, ret, senderAddr, senderAddrSize); } } } return 0; } void GetBCC(char * buf, int len) { char BCC = (char)0xAA; for(int i = 0;i < len-1;i++) { BCC = BCC ^ buf[i]; } buf[len-1] = BCC; } bool CheckBCC(char * buf, int len) { char BCC = (char)0xAA; for(int i = 0;i < len-1;i++) { BCC = BCC ^ buf[i]; } return (buf[len-1] == BCC); } bool GetIpList( list<string> & r_iplist ) { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { //cout < < "WSAStartup failed !" < < endl; return false; } char szhn[256]; int nStatus = gethostname(szhn, sizeof(szhn)); if (nStatus == SOCKET_ERROR ) { //cout < < "gethostname failed, Error code: " < < WSAGetLastError() < < endl; return false; } HOSTENT *host = gethostbyname(szhn); if (host != NULL) { for ( int i=0; ; i++ ) { r_iplist.push_back( inet_ntoa( *(IN_ADDR*)host-> h_addr_list[i] ) ) ; if ( host-> h_addr_list[i] + host-> h_length >= host-> h_name ) break; } } WSACleanup(); return true; }
评论
    相关推荐
    • UDPSocket.rar
      封装 UDP API 网络库。。有需要的朋友可以下来看看
    • c++ socket封装(udp/tcp)
      类c++封装的一个socket类,对新手还是有点帮助
    • socket UDP 简单练习
      NULL 博文链接:https://mzl-2011.iteye.com/blog/1151929
    • Socket-UDP整合消息接收,发送
      SocketUDP封装,包括配网,断网,接收UDP消息,发送UDP消息
    • UDP封装的几个类
      移动开发的一个封装包,还有测试工具。包含NetAssis工具,UDPApplication,UDPNegtive,UDPPositive,UDPTool等几个类。
    • TCPUDP Socket调试工具.zip
      socket的调试模拟工具,在网络体系结构中网络通信的建立必须是在通信双方的对等层进行,不能交错。 在整个数据传输过程中,数据在发送端时经过各层时都要附加上相应层的协议头和协议尾(仅数据链路层需要封装协议尾)...
    • UDP封装UDP封装
      和TCP的相对应,在运用UDP进行VC网络通信时能够用到。
    • vc编写UDP封装类(实用)
      vc编写UDP封装类(实用),注释很详尽,应用很方便。值得学习和参考的代码!
    • Android tcp-udp socket.zip
      socket实现客户端发送/接收消息 服务器接收/发送消息 支持udp客户端指定发送端口。代码简单 打开就可以运行
    • window下Socket和线程封装
      程序功能如下: 1、对UDP Socket进行封装; 2、接收和发送用同一个端口; 3、采用线程阻塞式接收; 4、程序清晰命了,集合Socket和线程封装,可以用来参考 ---因为同是UDP,则不能重复绑定,所以只能一次。