📄 mainfrm.cpp
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "Client.h"
#include "MainFrm.h"
//以下是添加的头文件
#include"ClientSocket.h"
#include"LinkDlg.h"
#include"MessageDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_NET_LINK, OnNetLink)
ON_COMMAND(ID_NET_SEND, OnNetSend)
ON_COMMAND(ID_NET_UNLINK, OnNetUnlink)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
//以下是构造函数的实现,进行成员变量的初始化工作
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
m_pSocket=NULL; //
m_strServerName=NULL;//
m_nPort=0;
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
//第一个菜单的响应函数:进行与服务器的连接设置
//===================================================================================
void CMainFrame::OnNetLink()
{
//首先判断是否与服务器进行了连接
if(m_pSocket)
{
MessageBox("已经连接到服务器了","错误信息",MB_OK);
return;
}
CLinkDlg m_LinkDlg;
if(m_LinkDlg.DoModal()==IDOK)
{
//得到需要连接的服务器的名字
m_strServerName=m_LinkDlg.m_strAddress;
//得到需要连接的服务器的端口号
m_nPort=m_LinkDlg.m_nPortNumber;
//创建客户端的用户Socket,同时传递框架指针
m_pSocket=new CClientSocket(this);
//进行和服务器的连接操作
if(!(m_pSocket=ConnectServer()))
{
//在连接失败时,要进行如下的处理:释放分配的空间
delete m_pSocket;
return;
}
}
}
//==================================================================================
void CMainFrame::OnNetSend()
{
// TODO: Add your command handler code here
if(!m_pSocket)
{
MessageBox("还没有进行服务器的连接","错误信息",MB_OK);
return;
}
CMessageDlg m_MessageDlg;
if(m_MessageDlg.DoModal())
{
char pMsg[10000];//发送数据时用到的变量
//得到要发送的信息
sprintf(pMsg,"%s",m_MessageDlg.m_strMessage.GetBuffer(10000));
//调用自定义的CSOCKET类的发送函数
m_pSocket->Send(pMsg,strlen(pMsg));
}
}
//===================================================================================
void CMainFrame::OnNetUnlink()
{
// TODO: Add your command handler code here
//判断是否已经和服务器建立了连接
if(m_pSocket)
{
delete m_pSocket;
m_pSocket=NULL;
}
else
MessageBox("还没有连接到服务器上","错误信息",MB_OK);
}
CClientSocket* CMainFrame::ConnectServer()//进行服务器连接的函数
{
CClientSocket *pTempSocket=new CClientSocket(this);
//生成客户端的Socket
if(!(pTempSocket->Create()))
{
//在生成套接字没有成功时需要进行的处理
delete pTempSocket;
MessageBox("生成套接字出错","错误信息",MB_OK);
return NULL;
}
if(!pTempSocket->Connect(m_strServerName,m_nPort))
{
//在套接字没有成功连接到服务器上时进行的处理
delete pTempSocket;
MessageBox("和服务器进行连接时出错","错误信息",MB_OK);
return NULL;
}
//如果都成功了,则把临时的Socket返回,把它人作为客户的socket
return pTempSocket;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -