?? chatroom2.cpp
字號:
// ChatRoom2.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "ChatRoom2.h"
#include "ChatRoom2Dlg.h"
#include "LoginDlg.h"
#include "ClientSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChatRoom2App
BEGIN_MESSAGE_MAP(CChatRoom2App, CWinApp)
//{{AFX_MSG_MAP(CChatRoom2App)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChatRoom2App construction
CChatRoom2App::CChatRoom2App()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CChatRoom2App object
CChatRoom2App theApp;
//發送線程函數
UINT SendDataThread(LPVOID lpParam);
//接收線程函數
UINT RecDataThread(LPVOID lpParam);
/////////////////////////////////////////////////////////////////////////////
// CChatRoom2App initialization
BOOL CChatRoom2App::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CChatRoom2Dlg chatdlg;
m_pMainWnd = &chatdlg;
if (chatdlg.DoModal() == IDOK)
{
// TODO: Place code here to handle when the dialog is
// // dismissed with OK
// CLoginDlg dlg;
// int nn;
// nn = dlg.DoModal();
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
//////////////////////////////////////////////////////////////////////////
//發送數據線程函數
UINT SendDataThread(LPVOID lpParam)
{
CChatRoom2Dlg *pDlg = (CChatRoom2Dlg *)lpParam;
CFile file;
CString str1,str2;
if (!file.Open(pDlg->m_strFilename,CFile::modeRead))
{
AfxMessageBox("打開文件出錯");
return 0;
}
pDlg->GetDlgItem(IDC_SENDFILE)->EnableWindow(FALSE);
pDlg->GetDlgItem(IDC_SENDEND)->EnableWindow(TRUE);
int iBufSize ;
iBufSize= 1024*5;
int iSize = iBufSize;
LPBYTE pBuf = new BYTE[iBufSize];
// DWORD dwTemp = 0;
// pDlg->m_sListenSocket.AsyncSelect(0);
// pDlg->m_sListenSocket.IOCtl(FIONBIO,&dwTemp);
UINT ulength = file.GetLength();
pDlg->m_sListenSocket.Send(&ulength,sizeof(UINT));
int iNumByte;
UINT uTotal = 0;
while (uTotal<ulength)
{
int iEnd;
iEnd = pDlg->m_bSendEnd;
iNumByte = pDlg->m_sListenSocket.Send(&iEnd,sizeof(int));
if (iNumByte==SOCKET_ERROR)
{
AfxMessageBox("發送出錯");
goto ExitSendData;
}
if (iEnd==1)
{
AfxMessageBox("發送端中止發送");
goto ExitSendData;
}
//讀取文件內容
if((int)(ulength-uTotal)<iBufSize)
iSize = ulength-uTotal;
iSize = file.Read(pBuf,iSize);
int iCount = 0;
//發送定長數據
while (iCount<iSize)
{
iNumByte = pDlg->m_sListenSocket.Send(pBuf,iSize-iCount);
if (iNumByte==SOCKET_ERROR)
{
AfxMessageBox("發送錯誤");
goto ExitSendData;
}
iCount += iNumByte;
if (iCount<iSize)
{
file.Seek(iSize-iCount,CFile::current);
}
}
uTotal += iCount;
//設置發送進度條
pDlg->m_proSend.SetPos(int(((double)uTotal/ulength)*100));
str1.Format("發送進度:%d%%",(int)(((double)uTotal/ulength)*100));
pDlg->GetDlgItemText(IDC_STATIC_SEND,str2);
if (str1!=str2)
{
pDlg->SetDlgItemText(IDC_STATIC_SEND,str1);
}
}
AfxMessageBox("文件發送成功");
ExitSendData:
delete[] pBuf;
file.Close();
pDlg->m_sListenSocket.Close();
pDlg->GetDlgItem(IDC_SENDEND)->EnableWindow(FALSE);
pDlg->GetDlgItem(IDC_SENDFILE)->EnableWindow(TRUE);
return 0;
}
//////////////////////////////////////////////////////////////////////////
//接收數據線程函數
UINT RecDataThread(LPVOID lpParam)
{
CChatRoom2Dlg *pDlg = (CChatRoom2Dlg *)lpParam;
CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"所有文件 (*.*)*.*");
while (dlg.DoModal()!=IDOK)
{
AfxMessageBox("文件選擇出錯,請重新選擇");
}
CString str1,str2,str3;
pDlg->GetDlgItem(IDC_RECEND)->EnableWindow(FALSE);
str3 = dlg.GetPathName();
CFile file;
file.Open(str3,CFile::modeCreate|CFile::modeWrite);
BOOL bFileFail = FALSE;
// DWORD dwTemp = 0;
// pDlg->m_cListenSocket.AsyncSelect(0);
// pDlg->m_cListenSocket.IOCtl(FIONBIO,&dwTemp);
UINT ulength;
pDlg->m_cListenSocket.Receive(&ulength,sizeof(UINT));
int iBufSize = 1024*5;
int iSize = iBufSize;
LPBYTE pBuf = new BYTE[iBufSize];
int iNumByte;
UINT uTotal = 0;
while (uTotal<ulength)
{
int iEnd = 0;
if (pDlg->m_bRecEnd)
{
AfxMessageBox("接收端中止");
goto ExitRecData;
}
iNumByte = pDlg->m_cListenSocket.Receive(&iEnd,sizeof(int));
if (iEnd==SOCKET_ERROR)
{
AfxMessageBox("接收數據出錯");
goto ExitRecData;
}
if (iEnd==1)
{
AfxMessageBox("發送端中止");
goto ExitRecData;
}
if ((int)(ulength-uTotal)<iBufSize)
{
iSize = ulength-uTotal;
}
int iCount = 0;
while (iCount<iSize)
{
iNumByte = pDlg->m_cListenSocket.Receive(pBuf,iSize-iCount);
if (iNumByte==SOCKET_ERROR)
{
AfxMessageBox("接收數據出錯");
goto ExitRecData;
}
iCount += iNumByte;
file.Write(pBuf,iNumByte);
}
uTotal += iCount;
pDlg->m_proRec.SetPos(int(((double)uTotal/ulength)*100));
pDlg->GetDlgItem(IDC_STATIC_REC)->GetWindowText(str2);
str1.Format("接收進度:%d%%",int(((double)uTotal/ulength)*100));
if(str1!=str2)
pDlg->SetDlgItemText(IDC_STATIC_REC,str1);
}
AfxMessageBox("接收數據成功");
bFileFail = FALSE;
ExitRecData:
delete[] pBuf;
file.Close();
if (!bFileFail)
{
CFile::Remove(str3);
}
pDlg->m_cListenSocket.Close();
pDlg->m_proRec.SetPos(0);
pDlg->GetDlgItem(IDC_SENDEND)->EnableWindow(TRUE);
pDlg->GetDlgItem(IDC_RECEND)->EnableWindow(TRUE);
pDlg->GetDlgItem(IDC_SENDFILE)->EnableWindow(TRUE);
pDlg->GetDlgItem(IDC_STATIC_REC)->SetWindowText("接收數據:");
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -