📄 lientchatdlg.cpp
字号:
// lientChatDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Client.h"
#include "lientChatDlg.h"
#include "ClientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// ClientChatDlg dialog
ClientChatDlg::ClientChatDlg(CWnd* pParent /*=NULL*/)
: CDialog(ClientChatDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(ClientChatDlg)
//}}AFX_DATA_INIT
}
void ClientChatDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(ClientChatDlg)
DDX_Control(pDX, IDC_LISTMSG, m_MSGS);
DDX_Control(pDX, IDC_EDITMSG, m_MSG);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(ClientChatDlg, CDialog)
//{{AFX_MSG_MAP(ClientChatDlg)
ON_BN_CLICKED(IDC_CLOSE_BUTTON, OnCloseButton)
ON_BN_CLICKED(IDC_SEND1_BUTTON, OnSend1Button)
ON_BN_CLICKED(IDC_SEND2_BUTTON, OnSend2Button)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// ClientChatDlg message handlers
void ClientChatDlg::OnCloseButton() //关闭客户端端函数
{
// TODO: Add your control notification handler code here
CClientApp* pApp=(CClientApp*)AfxGetApp();
CClientDlg* pDlg=(CClientDlg*)pApp->m_pMainWnd;
pDlg->m_socket->Close();//关闭客户端套接字
pDlg->m_socket=NULL;
OnOK();//关闭对话框
}
UINT ClientChatDlg::SendData1Thread(LPVOID lpParam)//发送消息线程
{
ClientChatDlg* pDlg1=(ClientChatDlg*)lpParam;
CClientApp* pApp=(CClientApp*)AfxGetApp();
CClientDlg* pDlg2=(CClientDlg*)pApp->m_pMainWnd;//获取主对话框类指针
if(pDlg2->m_connected)
{
pDlg2->m_socket->Send("M",2);
if(!pDlg1->m_MSG.GetWindowText(pDlg1->m_szBuffer,sizeof(pDlg1->m_szBuffer)))
return 0;
pDlg1->m_MSGS.InsertString(0,pDlg1->m_szBuffer);
pDlg1->m_MSG.SetWindowText("");
if(!pDlg2->m_socket->Send(pDlg1->m_szBuffer,strlen(pDlg1->m_szBuffer)))
{
AfxMessageBox("发送消息失败!");
}
}
return 0;
}
void ClientChatDlg::OnSend1Button() //发送消息按钮
{
// TODO: Add your control notification handler code here
AfxBeginThread(SendData1Thread,this,THREAD_PRIORITY_NORMAL);//发送消息线程
}
CString m_strFileName;//文件名
void ClientChatDlg::OnSend2Button() //发送文件线程
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"所有文件 (*.*)|*.*\0");
if(dlg.DoModal()==IDOK)
{
m_strFileName=dlg.GetPathName();
AfxBeginThread(SendData2Thread,this,THREAD_PRIORITY_NORMAL);//发送文件线程
}
}
UINT ClientChatDlg::SendData2Thread(LPVOID lpParam)//发送文件线程
{
CClientApp* pApp=(CClientApp*)AfxGetApp();
CClientDlg* pDlg=(CClientDlg*)pApp->m_pMainWnd;//获取主对话框类指针
pDlg->m_ChatDlg.GetDlgItem(IDC_SEND1_BUTTON)->EnableWindow(false);//设置发送消息不可用
pDlg->m_socket->Send("F",2);//F表示发送文件,通知服务器
Sleep(300);
CFile file;
FILEINFO myFileInfo;//定义文件信息对象
if(!file.Open(m_strFileName,CFile::modeRead))//判断打开文件是否成功
{
AfxMessageBox("打开文件出错!");
return 0;
}
myFileInfo.fileLength=file.GetLength();//获取文件长度
strcpy(myFileInfo.fileName,file.GetFileName());//获取文件名
int iBufSize=1024*5;//设置文件缓冲区长度
int iSize=iBufSize;//一次读取的长度
LPBYTE pBuf=new BYTE[iBufSize];//定义缓冲区
UINT uiLength=file.GetLength();//给文件长度复值
pDlg->m_socket->Send(&myFileInfo,sizeof(myFileInfo));//发送文件头信息
//***********************************
//判断服务器是否接受//30秒就超时300*100
CString str1,str2;//
int n=0;
char a[50];
str1=_T("对方已打开文件!");
str2=_T("");
while(strcmp(str1,str2)&&n<100)//相等就是已经就收
{
pDlg->m_ChatDlg.m_MSG.GetWindowText(a,50);
str2.Format(a);
Sleep(300);
n++;
}
if(n==100)
{
pDlg->m_ChatDlg.m_MSG.SetSel(0,100);
pDlg->m_ChatDlg.m_MSG.ReplaceSel(_T("对方接收文件超时!"));
return 0;
}
//***********************************
//***************************************
//发送文件
int iNumByte;
UINT uiTotal=0;
while(uiTotal<uiLength)
{
if((int)(uiLength-uiTotal)<iBufSize)
iSize=uiLength-uiTotal;
iSize=file.Read(pBuf,iSize);//读取文件到缓冲区
int iCount=0;
//while 循环发送独到的文件
while(iCount<iSize)
{
iNumByte=pDlg->m_socket->Send(pBuf,iSize-iCount);//发送剩下iSize-iCount的文件
if(iNumByte==SOCKET_ERROR)
{
AfxMessageBox("发送错误!");
return 0;
}
iCount+=iNumByte;
if(iCount<iSize)
{
file.Seek(iSize-iCount,CFile::current);//搜索文件开始发送的位置
}
}
uiTotal+=iCount;//uiTotal总发送数量
CString str;
str.Format( "%d%%",int(((double)uiTotal/uiLength)*100) );//显示发送进度
pDlg->m_ChatDlg.GetDlgItem(IDC_STATIC_SEND)->SetWindowText(str);
Sleep(5);
}
AfxMessageBox("发送文件成功");
pDlg->m_ChatDlg.m_MSG.SetSel(0,100);//选中100字符
pDlg->m_ChatDlg.m_MSG.ReplaceSel(_T(""));//晴空选中的字符
pDlg->m_ChatDlg.GetDlgItem(IDC_SEND1_BUTTON)->EnableWindow(true);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -