?? clientsocket.cpp
字號(hào):
// ClientSocket.cpp : implementation file
//
#include "stdafx.h"
#include "Sever.h"
#include "ClientSocket.h"
#include "SeverDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClientSocket
CClientSocket::CClientSocket()
{
}
CClientSocket::~CClientSocket()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
//{{AFX_MSG_MAP(CClientSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions
void CClientSocket::OnReceive(int nErrorCode) //接受按鈕函數(shù)
{
// TODO: Add your specialized code here and/or call the base class
char flag[2]={0};
if(Receive(flag,2)!=2)
return;
if(flag[0]=='F')
{
FILEINFO myFileInfo;
Receive(&myFileInfo,sizeof(myFileInfo));
CFileDialog dlg(FALSE,"123","456",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"所有文件 (*.*)|*.*\0");
if(dlg.DoModal()!=IDOK)
{
return ;
}
Send("Y",2);
Sleep(300);
CString str;
str.Format(myFileInfo.fileName);
CFile file;
file.Open(str,CFile::modeCreate|CFile::modeWrite);
UINT uiLength;
uiLength=(UINT)myFileInfo.fileLength;
int iBufSize=1024*5;
int iSize=iBufSize;
LPBYTE pBuf=new BYTE[iBufSize];
int iNumByte;
UINT uiTotal=0;
while(uiTotal<uiLength)
{
if((int)(uiLength-uiTotal)<iBufSize)
iSize=uiLength-uiTotal;
int iCount=0;
while(iCount<iSize)
{
iNumByte=Receive(pBuf,iSize-iCount);
if(iNumByte==SOCKET_ERROR)
{
AfxMessageBox("接收錯(cuò)誤!");
}
iCount+=iNumByte;
file.Write(pBuf,iNumByte);
}
uiTotal+=iCount;
CSeverApp* pApp=(CSeverApp*)AfxGetApp();
CSeverDlg* pDlg=(CSeverDlg*)pApp->m_pMainWnd;
CString str;
str.Format( "%d%%",int(((double)uiTotal/uiLength)*100) );
pDlg->GetDlgItem(IDC_STATIC_RECEIVE)->SetWindowText(str);
}
AfxMessageBox("接收文件成功!");
}
else if(flag[0]=='M')
{
char buffer[1024]={0};
int len=Receive(buffer,1024);
if(!len)
{
AfxMessageBox("接收信息異常!");
return;
}
CSeverApp* pApp=(CSeverApp*)AfxGetApp();
CSeverDlg* pDlg=(CSeverDlg*)pApp->m_pMainWnd;
pDlg->m_MSGG.InsertString(0,buffer);
POSITION pos=pDlg->m_list.GetHeadPosition(); //在CClientSocket類中,可通過主對(duì)話框指針方便的訪問定義在其中的套接字容器類!
while(pos!=NULL)
{
if(pos==pDlg->m_list.Find(this)) // m_list.Find(this)為找到當(dāng)前用來與客戶端通信的套接字!
{
pDlg->m_list.GetNext(pos);
continue;
}
CClientSocket* client=(CClientSocket*)pDlg->m_list.GetNext(pos);//返回pos所指位置的套接字,并使pos++
client->Send("M",2);
client->Send(buffer,strlen(buffer));
}
}
CSocket::OnReceive(nErrorCode);
}
void CClientSocket::OnClose(int nErrorCode) //關(guān)閉對(duì)話框
{
// TODO: Add your specialized code here and/or call the base class
CSeverApp* pApp=(CSeverApp*)AfxGetApp();
CSeverDlg* pDlg=(CSeverDlg*)pApp->m_pMainWnd;
POSITION pos = pDlg->m_list.Find(this);
if(pos != NULL)
{
pDlg->m_list.RemoveAt(pos);
}
CSocket::OnClose(nErrorCode);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -