// ClientSocket.cpp : implementation file
//
#include "stdafx.h" //預編譯頭檔案
#include "TransferDemo.h"
#include "ClientSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClientSocket
CClientSocket::CClientSocket(CTransferDemoDlg* pdlgMain)
{
m_pdlgMain = pdlgMain;
m_pFile = NULL;
m_pArchiveIn = NULL;
m_pArchiveOut = NULL;
}
CClientSocket::CClientSocket()
{
m_pdlgMain = NULL;
m_pFile = NULL;
m_pArchiveIn = NULL;
m_pArchiveOut = NULL;
}
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::Init()
{
m_pFile = new CSocketFile(this);
m_pArchiveIn = new CArchive(m_pFile,CArchive::load);
m_pArchiveOut = new CArchive(m_pFile,CArchive::store);
}
void CClientSocket::Abort()
{
if (m_pArchiveOut != NULL)
{
m_pArchiveOut->Abort();
delete m_pArchiveOut;
m_pArchiveOut = NULL;
}
}
BOOL CClientSocket::SendMsg(CMessage *pMsg)
{
if (m_pArchiveOut != NULL)
{
TRY
{
pMsg->Serialize(*m_pArchiveOut);
m_pArchiveOut->Flush();
return TRUE;
}
CATCH(CFileException, e)
{
m_pArchiveOut->Abort();
delete m_pArchiveOut;
m_pArchiveOut = NULL;
}
END_CATCH
}
return FALSE;
}
void CClientSocket::ReceiveMsg(CMessage *pMsg)
{
pMsg->Serialize(*m_pArchiveIn);
}
void CClientSocket::OnReceive(int nErrorCode)
{
m_pdlgMain->ProcessReceive(this);
CSocket::OnReceive(nErrorCode);
}
// ListenSocket.cpp : implementation file
//
#include "stdafx.h"
#include "TransferDemo.h"
#include "ListenSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CListenSocket
CListenSocket::CListenSocket(CTransferDemoDlg* pdlgMain)
{
m_pdlgMain = pdlgMain;
}
CListenSocket::CListenSocket()
{
m_pdlgMain = NULL;
}
CListenSocket::~CListenSocket()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CListenSocket, CSocket)
//{{AFX_MSG_MAP(CListenSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CListenSocket member functions
void CListenSocket::OnAccept(int nErrorCode)
{
m_pdlgMain->ProcessAccept();
CSocket::OnAccept(nErrorCode);
}
#include "stdafx.h"
#include "Message.h"
//默認的建構式
CMessage::CMessage()
{
m_nType = -1;
m_strFileName = _T("");
m_dwFileSize = 0;
}
//只需發送一般的控制資訊是使用
CMessage::CMessage(int nType)
{
m_nType = nType;
m_strFileName = _T("");
m_dwFileSize = 0;
}
//需要發送檔案名及大小時使用
CMessage::CMessage(int nType, CString strFileName, DWORD dwFileSize)
{
m_nType = nType;
m_strFileName = strFileName;
m_dwFileSize = dwFileSize;
}
CMessage::~CMessage()
{
}
void CMessage::Serialize(CArchive &ar)
{
if (ar.IsStoring())
{
ar << m_nType;
ar << m_strFileName;
ar << m_dwFileSize;
}
else
{
ar >> m_nType;
ar >> m_strFileName;
ar >> m_dwFileSize;
}
}
// TransferDemo.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "TransferDemo.h"
#include "TransferDemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTransferDemoApp
BEGIN_MESSAGE_MAP(CTransferDemoApp, CWinApp)
//{{AFX_MSG_MAP(CTransferDemoApp)
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTransferDemoApp construction
CTransferDemoApp::CTransferDemoApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CTransferDemoApp object
CTransferDemoApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CTransferDemoApp initialization
BOOL CTransferDemoApp::InitInstance()
{
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
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
CTransferDemoDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
uj5u.com熱心網友回復:
看樣子是Vc里面的代碼啊uj5u.com熱心網友回復:
知道這個,是那個簡單檔案傳輸的程式吧。。。uj5u.com熱心網友回復:
簡單點說就是用ClientSocket異步方式接收資料,用訊息佇列的方式控制同步。uj5u.com熱心網友回復:
我也剛接觸到 網路編程uj5u.com熱心網友回復:
就是一個簡單的檔案傳輸程式,當時是做課程設計,因為要講解,所以求注釋哈,還是謝謝了uj5u.com熱心網友回復:
你可以參考《WinSock網路編程經絡》第24章,有對FTP協議的詳細解釋,這里有FTP客戶端的實作,解壓后找FtpClnt,下載地址:http://download.csdn.net/detail/geoff08zhang/4571358uj5u.com熱心網友回復:
Thank you! 最近在學嵌入式,有沒有學過的啊uj5u.com熱心網友回復:
目測這個不能實作啊!轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/132774.html
標籤:網絡及通訊開發
上一篇:C++頭檔案疑惑
