📄 testdlg.cpp
字号:
// testDlg.cpp : implementation file
//
#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "Winsock2.h" //*****
#pragma comment(lib,"Ws2_32.lib") //*****
//////////////////// /////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
char lpData[1024]; //*****
int size; //*****
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_JOIN, OnJoin)
ON_MESSAGE(WM_MULTICAST, OnReceive) //*******
ON_BN_CLICKED(IDC_SEND, OnSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers
BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
SOCKADDR_IN addr;
SOCKADDR_IN saddr;
SOCKET m_hSocket;
SOCKET m_hGroupSocket;
void CTestDlg::OnJoin()
{
// TODO: Add your control notification handler code here
WSADATA wsa;
WSABUF wsaCall;
BOOL flag = TRUE; // 设置套接字为可重用端口地址;
WORD version = MAKEWORD(2, 0);
unsigned int port = 15000;
char IPAddr[20] = "234.5.5.5"; //设置多播组地址
int ret = 0;
int TTL = 16; //设置IP的TTL(Time To Live 数据生存期)
if(WSAStartup(version, &wsa)!=0)
{
AfxMessageBox("初始化错误!");
return;
}
m_hSocket = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED | WSA_FLAG_MULTIPOINT_C_LEAF | WSA_FLAG_MULTIPOINT_D_LEAF);
if (m_hSocket == INVALID_SOCKET)
{
AfxMessageBox("套接字错误!");
return;
}
if(SOCKET_ERROR == setsockopt(m_hSocket,SOL_SOCKET, SO_REUSEADDR, (char *)&flag,sizeof (flag)))
{
AfxMessageBox("设置套接字错误!");
return;
}
addr.sin_family = AF_INET;
addr.sin_port = htons (port);
addr.sin_addr.s_addr = INADDR_ANY;
// 将套接字绑扎到用户指定端口及默认的接口
if(SOCKET_ERROR == bind (m_hSocket, (struct sockaddr *)&addr, sizeof(struct sockaddr)))
{
AfxMessageBox("绑定套接字错误!");
return;
}
// 设置多址广播数据报传播范围(TTL)
if(SOCKET_ERROR == WSAIoctl(m_hSocket, SIO_MULTICAST_SCOPE, &TTL, sizeof (TTL), NULL, 0, (unsigned long*)&ret, NULL, NULL))
{
AfxMessageBox("设置TTL错误!");
return;
}
saddr.sin_family = AF_INET;
saddr.sin_port = htons (port);
saddr.sin_addr.s_addr = inet_addr(IPAddr);
m_hGroupSocket = WSAJoinLeaf(m_hSocket, (PSOCKADDR)&saddr,sizeof(saddr), NULL, &wsaCall, NULL, NULL, JL_BOTH);
if (m_hGroupSocket == INVALID_SOCKET)
{
AfxMessageBox("加入组失败!");
return;
}
if(SOCKET_ERROR == WSAAsyncSelect(m_hGroupSocket, m_hWnd, WM_MULTICAST, FD_WRITE | FD_READ | FD_QOS | FD_GROUP_QOS | FD_CONNECT))
{
AfxMessageBox("事件选择失败!");
return;
}
}
void CTestDlg::OnReceive(WPARAM wParam, LPARAM lParam) //******
{
WSABUF wsa;
CString str;
unsigned long ret = 0;
int len = sizeof (addr);
int flag = 0;
if(WSAGETSELECTERROR(lParam) != 0)
{
AfxMessageBox(" OnReceive 发生错误!");
return;
}
switch(WSAGETSELECTEVENT(lParam))
{
case FD_READ: //这里我们只对消息FD_READ感兴趣,所以只捕获它
wsa.buf = lpData;
wsa.len = 1024;
if(SOCKET_ERROR == WSARecvFrom(m_hGroupSocket, &wsa, 1,&ret, (unsigned long*)&flag, (struct sockaddr *)&addr,&len, NULL, NULL))
{
AfxMessageBox("接收数据出错!");
return;
}
str.Format("%s",lpData);
AfxMessageBox(str);
break;
default:
break;
}
}
void CTestDlg::OnSend()
{
// TODO: Add your control notification handler code here
unsigned long datasend = 0;
char data[6]="Hello";
WSABUF wsa;
wsa.len = 6;
wsa.buf = data;
if(SOCKET_ERROR == WSASendTo(m_hSocket, &wsa, 1, &datasend, 0, (LPSOCKADDR)&saddr, sizeof(SOCKADDR), NULL, NULL))
{
AfxMessageBox("发送数据出错!");
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -