📄 receiverview.cpp
字号:
// ReceiverView.cpp : implementation of the CReceiverView class
//
#include "stdafx.h"
#include "Receiver.h"
#include "ReceiverDoc.h"
#include "ReceiverView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_SOCKET_MSG WM_USER + 101
/////////////////////////////////////////////////////////////////////////////
// CReceiverView
IMPLEMENT_DYNCREATE(CReceiverView, CFormView)
BEGIN_MESSAGE_MAP(CReceiverView, CFormView)
//{{AFX_MSG_MAP(CReceiverView)
ON_BN_CLICKED(IDC_SET, OnSet)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
ON_MESSAGE(WM_SOCKET_MSG, OnSocket)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CReceiverView construction/destruction
CReceiverView::CReceiverView()
: CFormView(CReceiverView::IDD)
{
//{{AFX_DATA_INIT(CReceiverView)
m_sIP = _T("192.168.4.16");
m_nPort = 2000;
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CReceiverView::~CReceiverView()
{
}
void CReceiverView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CReceiverView)
DDX_Control(pDX, IDC_RECVDATA, m_listRecvData);
DDX_Text(pDX, IDC_IP, m_sIP);
DDX_Text(pDX, IDC_PORT, m_nPort);
//}}AFX_DATA_MAP
}
BOOL CReceiverView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CReceiverView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
// 创建套接字
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == INVALID_SOCKET)
{
CString sMSG;
sMSG.Format("创建套接字失败,错误码:%d", WSAGetLastError());
AfxMessageBox(sMSG);
}
}
/////////////////////////////////////////////////////////////////////////////
// CReceiverView printing
BOOL CReceiverView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CReceiverView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CReceiverView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CReceiverView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CReceiverView diagnostics
#ifdef _DEBUG
void CReceiverView::AssertValid() const
{
CFormView::AssertValid();
}
void CReceiverView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CReceiverDoc* CReceiverView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CReceiverDoc)));
return (CReceiverDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CReceiverView message handlers
BOOL CReceiverView::DestroyWindow()
{
// 关闭套接字
closesocket(sock);
// 卸载套接字
WSACleanup();
return CFormView::DestroyWindow();
}
void CReceiverView::OnSet()
{
UpdateData(TRUE);
// 设置协议族
local.sin_family = AF_INET;
// 设置端口
local.sin_port = htons(m_nPort);
// 设置IP地址
DWORD dwIPaddr = inet_addr(m_sIP);
if (dwIPaddr == INADDR_NONE)
AfxMessageBox("IP地址错误,请重新输入!");
else
local.sin_addr.S_un.S_addr = dwIPaddr;
// 建立绑定
if (bind(sock, (SOCKADDR*) &local, sizeof(local)) == SOCKET_ERROR)
{
CString sMSG;
sMSG.Format("建立绑定失败,错误码:%d", WSAGetLastError());
AfxMessageBox(sMSG);
}
// 设置异步选择
WSAAsyncSelect(sock, GetSafeHwnd(), WM_SOCKET_MSG, FD_READ);
}
void CReceiverView::OnSocket(WPARAM wParam, LPARAM lParam)
{
// 要显示的信息
CString sMessage;
// 接收缓冲区
char buf[1024];
memset(buf, 0, sizeof(buf));
// SOCKADDR_IN结构大小
int senderlen = sizeof(sender);
// 检测发生的网络事件
int message = lParam & 0x0000FFFF;
switch (message)
{
case FD_READ: // 读事件
// 读取数据
int ret = recvfrom(sock, buf, 1024, 0, (SOCKADDR*) &sender, &senderlen);
if (ret == SOCKET_ERROR)
{
CString sMSG;
sMSG.Format("接收数据失败,错误码:%d", WSAGetLastError());
AfxMessageBox(sMSG);
return;
}
// 将数据添加到列表控件
sMessage.Format("发送方IP:%s 接收数据:%s", CString(inet_ntoa(sender.sin_addr)), CString(buf));
m_listRecvData.AddString(sMessage);
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -