📄 netclientview.cpp
字号:
// NetClientView.cpp : CNetClientView 类实现文件
//
#include "stdafx.h"
#include "NetClient.h"
#include "clientSocket.h"
#include "NetClientDoc.h"
#include "NetClientView.h"
#include "commun.h" //通信连接信息
#include "dlg_InconnecInfo.h"//对话框
#include "msg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CNetClientView
IMPLEMENT_DYNCREATE(CNetClientView, CView)
BEGIN_MESSAGE_MAP(CNetClientView, CView)
//{{AFX_MSG_MAP(CNetClientView)
ON_COMMAND(ID_NET_CONNECT, OnNetConnect)
ON_UPDATE_COMMAND_UI(ID_NET_CONNECT, OnUpdateNetConnect)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CNetClientView 构造/销毁
CNetClientView::CNetClientView()
{
// TODO: 在此处添加构造代码
m_pSocket=NULL;
b_ConnectSocket=FALSE;
HFile_Receive.m_hFile=NULL;
}
CNetClientView::~CNetClientView()
{
}
BOOL CNetClientView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
// 样式
BOOL bPreCreated = CView::PreCreateWindow(cs);
cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL);
return bPreCreated;
}
// CNetClientView 绘制
void CNetClientView::OnDraw(CDC* pDC)
{
CNetClientDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: 在此处为本机数据添加绘制代码
if(!cList_msg.IsEmpty())
{ POSITION pos;
pos=cList_msg.GetHeadPosition();
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
int textheight=tm.tmHeight+2;
do
{
pDC->TextOut(10,textheight,cList_msg.GetNext(pos));
textheight+=textheight;
}
while(pos!=NULL);
}
}
// CNetClientView 打印
#ifdef _DEBUG
void CNetClientView::AssertValid() const
{
CView::AssertValid();
}
void CNetClientView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CNetClientDoc* CNetClientView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNetClientDoc)));
return (CNetClientDoc*)m_pDocument;
}
#endif //_DEBUG
// CNetClientView 消息处理程序
void CNetClientView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: 在此添加命令处理程序代码
if(!AfxSocketInit(&wsa_Connect))
{
//初始化socket库
AfxMessageBox(INIT_SOCKET_FAILED);
//发送初始化错误消息
return;
}
}
void CNetClientView::ReceiveFile(CClientSocket* pSocket)
{
//接收文件
TRY
{
CMsg msg;
CArchive* m_pArchive=pSocket->m_pArchiveIn;
do
{
msg.Init();
//接收信息
pSocket->ReceiveMsg(&msg);
if(!msg.m_bClose)
{
if(!msg.m_bFileClose)//文件内容
{
if(msg.n_Bytes>0)
HFile_Receive.Write(LPCTSTR(msg.m_strText),msg.n_Bytes);
}
else if(msg.m_bFileClose)//非文件信息
{
CDC* pDC=this->GetDC();
pDC->TextOut(10,10,msg.m_strText);
cList_msg.AddTail(msg.m_strText);
this->ReleaseDC(pDC);
}
}
else if(msg.m_bClose)//套接字关闭信息
{
CDC* pDC=this->GetDC();
pDC->TextOut(10,10,msg.m_strText);
cList_msg.AddTail(msg.m_strText);
this->ReleaseDC(pDC);
msg.Init();//初始化
msg.m_strText=IDS_THANKS;
msg.m_bClose=FALSE;
pSocket->SendMsg(&msg);
b_ConnectSocket=FALSE;//重置连接
if(m_pSocket !=NULL)
m_pSocket->ShutDown();
BYTE Buffer[50];
while (m_pSocket->Receive(Buffer,50) > 0);
delete m_pSocket;
m_pSocket=NULL;
while(!cList_msg.IsEmpty())
cList_msg.RemoveHead();
//关闭文件
if(HFile_Receive)
{
HFile_Receive.Close();
HFile_Receive.m_hFile=NULL;
}
}
}
while(!(m_pArchive->IsBufferEmpty()));
}
CATCH(CFileException,e)
{
AfxMessageBox("",MB_OK);
return;
}
END_CATCH
}
////////////////////////////////
//套接字连接
void CNetClientView::OnNetConnect()
{
// TODO: 在此添加命令处理程序代码
Dlg_InConnecInfo Dlg_ConnecInfo;
if(Dlg_ConnecInfo.DoModal()==IDOK)
{
str_UserName=Dlg_ConnecInfo.m_strName;//得到用户名
m_pSocket=new CClientSocket(this);
//创建连接套接字
if(!m_pSocket->Create())//Dlg_ConnecInfo.m_nPort,SOCK_STREAM,Dlg_ConnecInfo.m_strAddr
{
delete m_pSocket;
m_pSocket=NULL;
return;
}
//对于面向连接的用户,客户端调用Connect函数即可连接到服务器。
if(!m_pSocket->Connect(Dlg_ConnecInfo.m_strAddr,Dlg_ConnecInfo.m_nPort))
{
AfxMessageBox(IDS_CONNECTERROR);
return;
}
m_pSocket->Initialize();//初始化套接字
cList_msg.AddTail(IDS_CONNECT);
//发送给服务器登录消息
CMsg msg;
msg.Init();
msg.m_bClose = FALSE;
msg.m_strText = str_UserName+":"+IDS_CONNECT;
m_pSocket->SendMsg(&msg);
//打开保存文件名称
CString str_FileExt="Text Files (*.txt)|*.txt||";
CFileDialog dlg_GetFile(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,str_FileExt,NULL);
CString str_FilePath;
if(dlg_GetFile.DoModal()==IDOK)
{
str_FilePath=dlg_GetFile.GetPathName()+".txt";
}
else
return;
//发送文件名称信息,可以在此请求用户发送特定文件
msg.m_strText = str_UserName+":"+str_FilePath;
msg.m_bClose=FALSE;
m_pSocket->SendMsg(&msg);
//创建并打开文件
TRY
{
CFileException e;
if(!(HFile_Receive.Open(str_FilePath,CFile::modeCreate|CFile::modeWrite,&e)))
{
AfxMessageBox(IDS_OPENFILEERROR);
return;
}
}
CATCH(CFileException,e)
{
return;
}
END_CATCH
}
b_ConnectSocket=TRUE;
}
void CNetClientView::OnUpdateNetConnect(CCmdUI* pCmdUI)
{
// TODO: 在此添加命令处理程序代码
pCmdUI->Enable(b_ConnectSocket==FALSE);
}
void CNetClientView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: 在此添加命令处理程序代码
pDC->SetMapMode(MM_TEXT);
pDC->SetWindowOrg(10,10);
CView::OnPrepareDC(pDC, pInfo);
}
void CNetClientView::OnDestroy()
{
CView::OnDestroy();
if(HFile_Receive.m_hFile!=NULL)
HFile_Receive.Close();
// TODO: 在此添加命令处理程序代码
if (m_pSocket != NULL)
{
if((m_pSocket->m_pFile!= NULL) && (m_pSocket->m_pArchiveOut != NULL))
{
CMsg msg;
msg.Init();
msg.m_bClose = TRUE;
msg.m_strText = str_UserName+":"IDS_THANKS;
m_pSocket->SendMsg(&msg);
}
}
if(m_pSocket !=NULL)
{
m_pSocket->ShutDown();
BYTE Buffer[50];
while (m_pSocket->Receive(Buffer,50) > 0);
delete m_pSocket;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -