?? sendstudentview.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 + -