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

📄 rcvstudentview.cpp

📁 通过网络接收数据并保存到数据库 通过网络接收数据并保存到数据库
💻 CPP
字号:
// RcvStudentView.cpp : implementation of the CRcvStudentView class
//

#include "stdafx.h"
#include "RcvStudent.h"

#include "RcvStudentSet.h"
#include "RcvStudentDoc.h"
#include "RcvStudentView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CRcvStudentView

IMPLEMENT_DYNCREATE(CRcvStudentView, CRecordView)

BEGIN_MESSAGE_MAP(CRcvStudentView, CRecordView)
	//{{AFX_MSG_MAP(CRcvStudentView)
	ON_BN_CLICKED(IDC_BT_RECEIVE_FROM_COM2, OnBtReceiveFromCom2)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BT_SAVE_TO_DB, OnBtSaveToDb)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRcvStudentView construction/destruction

CRcvStudentView::CRcvStudentView()
	: CRecordView(CRcvStudentView::IDD)
{
	//{{AFX_DATA_INIT(CRcvStudentView)
	m_pSet = NULL;
	m_nID = 0;
	m_strName = _T("");
	m_strPhone = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CRcvStudentView::~CRcvStudentView()
{
}

void CRcvStudentView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRcvStudentView)
	DDX_Text(pDX, IDC_EDIT_ID, m_nID);
	DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
	DDV_MaxChars(pDX, m_strName, 19);
	DDX_Control(pDX, IDC_IPADDRESS_DEST, m_ipDest);
	DDX_Text(pDX, IDC_EDIT_PHONE, m_strPhone);
	DDV_MaxChars(pDX, m_strPhone, 19);
	//}}AFX_DATA_MAP
}

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

	return CRecordView::PreCreateWindow(cs);
}

void CRcvStudentView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_rcvStudentSet;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	//初始化Socket
	AfxSocketInit(NULL);
	m_ipDest.SetWindowText("127.0.0.1");//设置默认IP地址
}

/////////////////////////////////////////////////////////////////////////////
// CRcvStudentView diagnostics

#ifdef _DEBUG
void CRcvStudentView::AssertValid() const
{
	CRecordView::AssertValid();
}

void CRcvStudentView::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CRcvStudentView database support
CRecordset* CRcvStudentView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CRcvStudentView message handlers

void CRcvStudentView::OnBtReceiveFromCom2() 
{
	CSocket serSock;

	//本机IP和端口号设置
	DWORD error;
	char ip[20] = "127.0.0.1";
	int port = 6811;

	BOOL bFinished=FALSE;
	bFinished=serSock.Create(port,SOCK_STREAM,ip);
	if(bFinished==FALSE) {
		error=GetLastError();
		return;
	}
	//服务器ip和端口号设置
	char ip_ser[20];	
	CString strip;
	m_ipDest.GetWindowText(strip);
	strcpy(ip_ser, strip);

	int port_ser = 6833; 

	bFinished=FALSE;
	bFinished=serSock.Connect(ip_ser,port_ser);
	if(bFinished==FALSE) {
		error=GetLastError();
		return;
	}
	TRACE("Connect finished!");

	serSock.Receive((BYTE*)&m_nID,sizeof(m_nID));

	BYTE buf[20] = {0};

	serSock.Receive(buf,sizeof(buf));
	m_strName = (char*)buf;

	serSock.Receive(buf,sizeof(buf));
	m_strPhone = (char*)buf;

	serSock.Close();
	UpdateData (FALSE);

}

void CRcvStudentView::OnDestroy() 
{
	CRecordView::OnDestroy();
}

void CRcvStudentView::OnBtSaveToDb() 
{
	m_pSet->AddNew();
	m_pSet->m_StudentID = m_nID;
	m_pSet->m_StudentName = m_strName;
	m_pSet->m_Telephone = m_strPhone;
	m_pSet->Update();
}

⌨️ 快捷键说明

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