📄 netview.cpp
字号:
// netView.cpp : CNetView 实现文件
//
#include "stdafx.h"
#include "net.h"
#include "listensocket.h"
#include "receivesocket.h"
#include "netDoc.h"
#include "netView.h"
#include "InputPort.h"
#include "commun.h" //连接信息
#include "msg.h" //交换信息结构
#include <iostream.h>//发送流
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CNetView
IMPLEMENT_DYNCREATE(CNetView, CView)
BEGIN_MESSAGE_MAP(CNetView, CView)
//{{AFX_MSG_MAP(CNetView)
ON_COMMAND(ID_NET_SENDDATA, OnNetSenddata)
ON_UPDATE_COMMAND_UI(ID_NET_SENDDATA, OnUpdateNetSenddata)
ON_COMMAND(ID_NET_SERVERSTART, OnNetServerstart)
ON_UPDATE_COMMAND_UI(ID_NET_SERVERSTART, OnUpdateNetServerstart)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
// ON_THREAD_MESSAGE(MM_LISTENACCEPT,OnListenAccept)
END_MESSAGE_MAP()
// CNetView 构造/销毁
CNetView::CNetView()
{
// TODO: 在此添加构造代码
m_pSocket=NULL;
b_Listening=FALSE;
pp_Socket=NULL;
}
CNetView::~CNetView()
{
}
BOOL CNetView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
// 样式
return CView::PreCreateWindow(cs);
}
// CNetView绘制
void CNetView::OnDraw(CDC* pDC)
{
CNetDoc* 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+8;
int n_Line=10;
do
{
pDC->TextOut(10,textheight,cList_msg.GetNext(pos));
n_Line+=textheight;
}
while(pos!=NULL);
}
}
// CNetView 打印
BOOL CNetView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void CNetView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 打印前添加额外的初始化
}
void CNetView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 打印后添加清除过程
}
// CNetView诊断
#ifdef _DEBUG
void CNetView::AssertValid() const
{
CView::AssertValid();
}
void CNetView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CNetDoc* CNetView::GetDocument() // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNetDoc)));
return (CNetDoc*)m_pDocument;
}
#endif //_DEBUG
// CNetView 消息处理程序
void CNetView::OnNetSenddata()
{
// TODO: 在此添加命令处理程序代码
SendFile(pp_Socket);
}
void CNetView::OnUpdateNetSenddata(CCmdUI* pCmdUI)
{
// TODO: 在此添加命令处理程序代码
pCmdUI->Enable(!(ClientSocket_Oblist.IsEmpty()));
}
void CNetView::OnNetServerstart()
{
// TODO: 在此添加命令处理程序代码
CInPutPort Dlg_InputPort;
if (Dlg_InputPort.DoModal() == IDOK)
{
//建立侦听Socket
m_pSocket = new CListenSocket(this);
if (m_pSocket->Create(Dlg_InputPort.m_nInPort))
{
if (m_pSocket->Listen())
{
b_Listening=TRUE;
//开始侦听
return ;
}
}
}
AfxMessageBox("WRONG");
}
void CNetView::OnUpdateNetServerstart(CCmdUI* pCmdUI)
{
// TODO: 在此添加命令处理程序代码
pCmdUI->Enable(b_Listening==FALSE);
}
/////接收客户连接请求]
void CNetView::OnListenAccept(WPARAM wParam,LPARAM lParam)
{
CReceiveSocket* pSocket = new CReceiveSocket(this);
pp_Socket=pSocket;
SOCKADDR* lpSockAddr=new SOCKADDR;
int n_Byte=sizeof(SOCKADDR);
if (m_pSocket->Accept(*pSocket,lpSockAddr,&n_Byte))
{
//初始化接收套接字
pSocket->Initialize();
ClientSocket_Oblist.AddHead(pSocket);
TRY
{ //发送欢迎信息
CMsg msg;
msg.Init();
CString s1="服务器:";
msg.m_strText=s1+IDS_WELCOME;
msg.m_bFileClose=TRUE;
pSocket->SendMsg(&msg);
}
CATCH(CFileException,e)
{
pSocket->Abort();
AfxMessageBox(IDS_SENDERROR);
pp_Socket=NULL;
}
END_CATCH
}
else
{
delete pSocket;
pp_Socket=NULL;
}
}
//接收客户端消息
void CNetView::ReceiveData(CReceiveSocket *pSocket)
{
CMsg msg;
msg.Init();
TRY
{
pSocket->ReceiveMsg(&msg);
//仅区分客户关闭套接字信息
if(msg.m_bClose)
{
cList_msg.AddTail(msg.m_strText);
//关闭与客户端通信的套接字
CloseSocket(pSocket);
}
else
cList_msg.AddTail(msg.m_strText);
Invalidate(TRUE);
}
CATCH(CFileException, e)
{
pSocket->Abort();
AfxMessageBox(IDS_RECEIVEERROR);
}
END_CATCH
}
void CNetView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
WORD w_Version;
w_Version=MAKEWORD( 1, 1);//版本号码,当前Windows Socket的版本为1.1
if(WSAStartup(w_Version,&wsa_Now)!=0)
{
AfxMessageBox(INIT_SOCKET_FAILED,MB_OK);//初始化错误
return;
}
}
//关闭套接字
void CNetView::CloseSocket(CReceiveSocket *pSocket)
{
pSocket->Close();
//查找、删除链表内套接字指针
POSITION pos,temp;
for(pos = ClientSocket_Oblist.GetHeadPosition(); pos != NULL;)
{
temp = pos;
CReceiveSocket* pSock = (CReceiveSocket*)ClientSocket_Oblist.GetNext(pos);
if (pSock == pSocket)
{
ClientSocket_Oblist.RemoveAt(temp);
break;
}
}
delete pSocket;
}
//关闭服务器
void CNetView::OnDestroy()
{
CView::OnDestroy();
// TODO:在此添加命令处理程序代码
CMsg msg;
msg.Init();
msg.m_bClose=TRUE;
msg.m_bFileClose=TRUE;
//服务器关闭信息
CString s1="服务器:";
msg.m_strText=s1+SERVER_SHUTDOWN;
//服务器向每个客户端发送服务器关闭信息
while(!ClientSocket_Oblist.IsEmpty())
{
CReceiveSocket* temp;
temp=(CReceiveSocket*)ClientSocket_Oblist.RemoveHead();
temp->SendMsg(&msg);
delete temp;
}
if(m_pSocket!=NULL)
{
m_pSocket->Close();
delete m_pSocket;
m_pSocket=NULL;
};
WSACleanup();
}
void CNetView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: 在此添加命令处理程序代码
pDC->SetMapMode(MM_TEXT);
pDC->SetWindowOrg(10,10);
CView::OnPrepareDC(pDC, pInfo);
}
void CNetView::SendFile(CReceiveSocket *pSocket)
{
TRY
{
//获取文件路径及文件名
CString str_FileExt="Text Files (*.txt)|*.txt||";
CFileDialog dlg_FileOpen(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,str_FileExt,NULL);
CString Str_Fullfile;
if(dlg_FileOpen.DoModal()==IDOK)
{
Str_Fullfile=dlg_FileOpen.GetPathName();
}
else
return;
//发送文件
CFileException cexep_Now;
CFile m_FileOut;//(Str_Fullfile,CFile::modeRead);
UINT n_ReadFile=0;
n_ReadFile=m_FileOut.Open(Str_Fullfile,CFile::modeRead,&cexep_Now);
if(n_ReadFile == 0)
{
AfxMessageBox(IDS_OPENFILEERROR);
return;
}
char str_text[66];
CMsg msg;
CArchive* m_pArchive=pSocket->m_pArchiveOut;//用于发送归档对象
//设置初值
n_ReadFile=0;
do
{
for(int i=0;i<66;i++)
str_text[i]=32;
n_ReadFile=m_FileOut.Read(str_text,66);
if(n_ReadFile>0)
{
msg.Init();
msg.m_strText=str_text;
msg.n_Bytes=n_ReadFile;
pSocket->SendMsg(&msg);
}
}
while(n_ReadFile>0);
//发送文件结束
m_FileOut.Close();
if(n_ReadFile<66)
{
msg.Init();
CString s1="服务器:";
msg.m_strText=s1+IDS_SENDCOMPLETED;
msg.m_bFileClose=TRUE;
pSocket->SendMsg(&msg);
}
}
CATCH(CFileException,e)
{
AfxMessageBox("文件打开错误",MB_OK);
return;
}
END_CATCH
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -