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

📄 sendstudentview.cpp

📁 把数据库中已经存在的数据通过网络发送出去
💻 CPP
字号:
// SendStudentView.cpp : implementation of the CSendStudentView class
//

#include "stdafx.h"
#include "SendStudent.h"

#include "SendStudentSet.h"
#include "SendStudentDoc.h"
#include "SendStudentView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSendStudentView

IMPLEMENT_DYNCREATE(CSendStudentView, CRecordView)

BEGIN_MESSAGE_MAP(CSendStudentView, CRecordView)
	//{{AFX_MSG_MAP(CSendStudentView)
	ON_BN_CLICKED(IDC_BT_SEND_TO_COM1, OnBtSendToCom1)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSendStudentView construction/destruction

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

}

CSendStudentView::~CSendStudentView()
{
}

void CSendStudentView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSendStudentView)
	DDX_Text(pDX, IDC_EDIT_ID, m_nID);
	DDV_MinMaxLong(pDX, m_nID, 0, 65535);
	DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
	DDV_MaxChars(pDX, m_strName, 19);
	DDX_Text(pDX, IDC_EDIT_PHONE, m_strPhone);
	DDV_MaxChars(pDX, m_strPhone, 19);
	//}}AFX_DATA_MAP
}

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

	return CRecordView::PreCreateWindow(cs);
}

void CSendStudentView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_sendStudentSet;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	//初始化Socket
	AfxSocketInit(NULL);
	m_pSet->MoveFirst();
	m_nID=m_pSet->m_StudentID;
	m_strName=m_pSet->m_StudentName;
	m_strPhone=m_pSet->m_Telephone;
	UpdateData(FALSE);
}

/////////////////////////////////////////////////////////////////////////////
// CSendStudentView diagnostics

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

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

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

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


/////////////////////////////////////////////////////////////////////////////
// CSendStudentView message handlers

BOOL CSendStudentView::OnMove(UINT nIDMoveCommand) 
{
	// TODO: Add your specialized code here and/or call the base class

	if (CRecordView::OnMove(nIDMoveCommand)){
		m_nID = m_pSet->m_StudentID;
		m_strName = m_pSet->m_StudentName;
		m_strPhone = m_pSet->m_Telephone;
		UpdateData (FALSE);
		return TRUE;
	}
	else{
		return FALSE;
	}

	return FALSE;
}

void CSendStudentView::OnBtSendToCom1() 
{
	//建立Socket:
	CSocket serSock;
	BOOL bFinished = FALSE;
	DWORD error;
	//设置服务器ip和端口
	//****************单机调试*******************
	char ip[20] = "127.0.0.1";
    int port = 6833;
	//*******************************************
	/**************真正用于发送数据*************
    char ip[20];
	int port = 6833;
	CString strLocalName;//用于存放计算机名
	GetLocalHostName(strLocalName);//得到计算机名
	CString strLocalIP;//用于存放IP地址
	GetIpAddress(strLocalName,strLocalIP);//通过计算机名获得IP地址
	strcpy(ip,strLocalIP);
    *********************************************/
	bFinished = serSock.Create(port, SOCK_STREAM, ip);
	if(bFinished==FALSE) {
		error=GetLastError();
		return;
	}

	//监听
	bFinished=FALSE;
	bFinished=serSock.Listen(5);
	if(bFinished==FALSE) {
		error=GetLastError();
		return;
	}
	bFinished=FALSE;
	CSocket aSocket;
	//建立连接
	bFinished=serSock.Accept(aSocket);
	if(bFinished==FALSE) {
		error=GetLastError();
		return;
	}
	TRACE("Accept finished!");
	serSock.Close();
	//发送数据
	aSocket.Send((BYTE*)&m_nID, sizeof(m_nID));

	BYTE buf[20] = {0};

	strcpy ((char*)buf, m_strName.GetBuffer(m_strName.GetLength()));
	aSocket.Send(buf, sizeof(buf));

	strcpy ((char*)buf, m_strPhone.GetBuffer(m_strPhone.GetLength()));
	aSocket.Send(buf,sizeof(buf));

	aSocket.Close();

	AfxMessageBox("Send Finished!");
}

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

int CSendStudentView::GetLocalHostName(CString &sHostName)
{
	char szHostName[256];
	int nRetCode;
	nRetCode=gethostname(szHostName,sizeof(szHostName));
	if(nRetCode!=0)
	{
		//产生错误
		sHostName=_T("没有取得");
		return GetLastError();
	}
	sHostName=szHostName;
	return 0;

}

int CSendStudentView::GetIpAddress(const CString &sHostName, CString &sIpAddress)
{
	struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
	if(lpHostEnt==NULL)
	{
		//产生错误
		sIpAddress=_T("");
		return GetLastError();
	}
	//获取IP
	LPSTR lpAddr=lpHostEnt->h_addr_list[0];
	if(lpAddr)
	{
		struct in_addr inAddr;
		memmove(&inAddr,lpAddr,4);
		//转换为标准格式
		sIpAddress=inet_ntoa(inAddr);
		if(sIpAddress.IsEmpty())
			sIpAddress=_T("没有取得");
	}
	return 0;

}

⌨️ 快捷键说明

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