⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 senderview.cpp

📁 visual c++网络通信程序开发指南附带的程序所有的源码。
💻 CPP
字号:
// SenderView.cpp : implementation of the CSenderView class
//

#include "stdafx.h"
#include "Sender.h"

#include "SenderDoc.h"
#include "SenderView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSenderView

IMPLEMENT_DYNCREATE(CSenderView, CFormView)

BEGIN_MESSAGE_MAP(CSenderView, CFormView)
	//{{AFX_MSG_MAP(CSenderView)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_BN_CLICKED(IDC_SETADDR, OnSetaddr)
	//}}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)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSenderView construction/destruction

CSenderView::CSenderView()
	: CFormView(CSenderView::IDD)
{
	//{{AFX_DATA_INIT(CSenderView)
	m_sData = _T("");
	m_nPort = 2000;
	m_sIP = _T("192.168.4.16");
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CSenderView::~CSenderView()
{
}

void CSenderView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSenderView)
	DDX_Text(pDX, IDC_DATA, m_sData);
	DDX_Text(pDX, IDC_PORT, m_nPort);
	DDX_Text(pDX, IDC_IP, m_sIP);
	//}}AFX_DATA_MAP
}

BOOL CSenderView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CSenderView::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);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CSenderView printing

BOOL CSenderView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CSenderView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CSenderView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CSenderView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CSenderView diagnostics

#ifdef _DEBUG
void CSenderView::AssertValid() const
{
	CFormView::AssertValid();
}

void CSenderView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CSenderDoc* CSenderView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSenderDoc)));
	return (CSenderDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSenderView message handlers

void CSenderView::OnSend() 
{
	UpdateData(TRUE);

	// 发送数据
	int ret = sendto(sock, m_sData, m_sData.GetLength(), 0, (SOCKADDR*) &receiver, sizeof(receiver));
	if (ret == SOCKET_ERROR)
	{
		CString sMSG;
		sMSG.Format("发送数据失败,错误码:%d", WSAGetLastError());
		AfxMessageBox(sMSG);
	}
}

void CSenderView::OnSetaddr() 
{
	UpdateData(TRUE);
	
	// 设置协议族
	receiver.sin_family = AF_INET;

	// 设置端口号
	receiver.sin_port = htons(m_nPort);

	// 设置IP地址
	DWORD dwIPaddr = inet_addr(m_sIP);
    if (dwIPaddr == INADDR_NONE)
		AfxMessageBox("IP地址错误,请重新输入!");
	else
		receiver.sin_addr.S_un.S_addr = dwIPaddr;
}

BOOL CSenderView::DestroyWindow() 
{
	// 关闭套接字
	closesocket(sock);	

	// 卸载套接字
	WSACleanup();
	return CFormView::DestroyWindow();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -