【VC开源代码栏目提醒】:以下是网学会员为您推荐的VC开源代码-Client1.cpp,希望本篇文章对您学习有所帮助。
// client1.cpp : Defines the class behaviors for the application.
//*************************************************************
//作者:赵明
//EMAIL:zmpapaya@hotmail.com;papaya_zm@sina.com
//主页:http://h2osky.126.com
/********************************************************/
#include "stdafx.h"
#include "client1.h"
#include "linkctrl.h"
#include "MainFrm.h"
#include "client1Doc.h"
#include "client1View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClient1App
BEGIN_MESSAGE_MAP(CClient1App, CWinApp)
//{{AFX_MSG_MAP(CClient1App)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClient1App construction
CClient1App::CClient1App()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CClient1App object
CClient1App theApp;
//这仅仅是一个过渡性的线程函数。
DWORD WINAPI notify(LPVOID lpparam)
{
CMainFrame* pthis=(CMainFrame*)lpparam;
//文件的下载操作完成之后的处理。
return pthis->finish();
}
//
DWORD WINAPI timethread(LPVOID lpparam)
{
//记录的明明是cdownload类的数据的this指针嘛。
cdownload* pthis=(cdownload*)lpparam;
//真正的代码在cdownload类的成员函数finish1中。之所以要用timethread函数来作为中间的过渡,
//是因为线程函数只能使用普通函数,而不能使用类的成员函数,所以,就只好这么设计了。
return pthis->finish1();
}
//参数1:连接socket;参数2:接受数据的缓冲区;参数3:要下载的字节数。
//从服务器端接受一定数量的数据。
int readn(SOCKET fd,char *bp,int len)
{
int cnt;//用来保存剩余的字节数。
int rc;
cnt=len;
while(cnt>0)
{
//Return Values If no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
rc=recv(fd,bp,cnt,0);
//如果出错了,则
if(rc==SOCKET_ERROR)
{
CString aaa;
aaa="接收错误\n";
AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)aaa.GetBuffer(0),1);
aaa.ReleaseBuffer();
return SOCKET_ERROR;
}
//如果连接has been gracefully closed,则
if(rc==0)
return len-cnt;//返回的是已经成功下载了的字节数。
bp+=rc;
cnt-=rc;
}
return len;
}
//发送数据给socket连接的另一端。
int sendn(SOCKET fd,char *bp,int len)
{
int cnt;
int rc;
cnt=len;//要发送的字节数,即Length of the data in buf.
//用下面的这个while循环来保证,要发送的数据全部都被发送出去了。
while(cnt>0)
{
rc=send(fd,bp,cnt,0);
if(rc<0)
{
CString aaa;
aaa="发送错误\n";
AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)aaa.GetBuffer(0),1);
aaa.ReleaseBuffer();
return -1;
}
if(rc==0)
return len-cnt;
//发送完一批数据后,缓冲区指针要改变,
bp+=rc;
//Length of the data in buf也要变。
cnt-=rc;
}
return len;
}
CString g_csIP;
//过渡性的线程函数。
//参数:是一个指向down[threadno]的指针。
DWORD WINA