📄 udp.cpp
字号:
// UDP.cpp : implementation file
//
#include "stdafx.h"
#include "UDP.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUDP
CUDP::CUDP(CWnd* pWnd,UINT UserMessageID,int MaxPackageSize)
{
m_MaxPackageSize=MaxPackageSize;
m_UserMessageID=UserMessageID;
m_pWnd=pWnd;
int i;
for(i=0;i<MAX_BUFFER_NUM;i++)
{
m_Udp_Buffer[i].pData=new unsigned char [m_MaxPackageSize];
}
m_Index=0;
InitializeCriticalSection(&UDPCriticalSection);
}
CUDP::~CUDP()
{
int i;
for(i=0;i<MAX_BUFFER_NUM;i++)
{
if(m_Udp_Buffer[i].pData) delete m_Udp_Buffer[i].pData;
}
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CUDP, CAsyncSocket)
//{{AFX_MSG_MAP(CUDP)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CUDP member functions
void CUDP::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CString client;
UDPPackage *pData;
EnterCriticalSection(&UDPCriticalSection);
pData=&m_Udp_Buffer[m_Index];
m_Index=(m_Index+1)%MAX_BUFFER_NUM;
pData->len=ReceiveFrom(pData->pData,m_MaxPackageSize,client,pData->port,0);
strcpy(pData->ip,client.GetBuffer(32));
if(m_pWnd) m_pWnd->PostMessage(m_UserMessageID,(UINT)pData,(UINT)sizeof(UDPPackage));
LeaveCriticalSection(&UDPCriticalSection);
CAsyncSocket::OnReceive(nErrorCode);
}
void CUDP::OnSend(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CAsyncSocket::OnSend(nErrorCode);
}
CString CUDP::GetHostIP()
{
WORD wversionrequested;
WSADATA wsadata;
char name[255];
CString ip="IP";
PHOSTENT hostinfo;
wversionrequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wversionrequested, &wsadata ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}
WSACleanup( );
}
return ip;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -