⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chat.cpp

📁 点对点传输文件的程序。使用TransmitFile调用
💻 CPP
字号:
// Chat.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Chat.h"
#include "ChatDlg.h"
#include "FileTransfer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////
DWORD WINAPI RecvThreadFunc(LPVOID lp)
{
CChatDlg *p=(CChatDlg *)lp;

char buf[1024];
    unsigned long recvlen=0;
    unsigned long prefilelen=0;
	char *fname;
	SOCKADDR_IN peer;
    int namelen=sizeof(peer);
	int len;
	listen(p->m_mylisten,1);
loop:
	p->s=accept(p->m_mylisten,(struct sockaddr*)&peer,&namelen);	
	(p->GetDlgItem(IDC_CONNECT))->EnableWindow(false);
    	(p->GetDlgItem(IDC_SHUTCLOSE))->EnableWindow(true);
	::MessageBox(p->m_hWnd,"接受了一个连接请求!","",MB_OK);
    //p->GetDlgItem(IDC_BUTTON2)->EnableWindow(true);
    p->GetDlgItem(IDOK)->EnableWindow(true);
    p->m_theirip.SetAddress(ntohl(peer.sin_addr.S_un.S_addr));
	p->SetDlgItemInt(IDC_PORT,ntohs(peer.sin_port));
loop1:    
	// recvlen=0;
	int llen=0;
	while(llen<8)
	{
		len=0;
		len=recv(p->s,buf+llen,1024,0);
        llen+=len;
	}
    buf[llen]='\0';
    prefilelen=*((unsigned long *)buf);
	fname=NULL;
	len=*( (int*) (buf+4) );
	fname=new char[len+1];
	if(fname==NULL)
	{
     ::MessageBox(p->m_hWnd,"生成文件名出错!","信息确认",MB_OK);
	  AfxEndThread(100) ;
	}
	strcpy(fname,(char*)(buf+8));
	fname[len]='\0';
    CString fileinfo=_T("");
	fileinfo.Format("对方发来文件:\n%s\n是否接收?",fname);

   	int r=::MessageBox(NULL,fileinfo.GetBuffer(0),"信息确认",MB_YESNO);
    if(r==IDNO)
	{
		delete []fname;
		closesocket(p->s);
		goto loop1;
	}
	fileinfo=_T("");
	fileinfo.Format("C:\download\\%s",fname);
    ::MessageBox(NULL,"接收路径设置完毕!","信息确认",MB_OK);
// 	HANDLE hfile=CreateFile(fileinfo.GetBuffer(0),GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
	CFile recfile(fileinfo.GetBuffer(0),CFile::modeCreate|CFile::shareDenyWrite|CFile::modeWrite);
    ::MessageBox(NULL,"文件创建完毕!","信息确认",MB_OK);
	delete []fname;
	unsigned long totallen=0;

	while(totallen!=prefilelen)
	{  // ::MessageBox(NULL,fileinfo.GetBuffer(0),"信息确认",MB_OK);
      ZeroMemory(buf,1024);
	  // recvlen=0;
	  len=0;
	  len=recv(p->s,buf,1024,0);
      // recvlen+=len;	 
	  totallen+=len;
	  recfile.Write(buf,len);

	}
	// recfile.Close();
	::MessageBox(p->m_hWnd,"文件接收完毕!","信息确认",MB_OK);
	closesocket(p->s);
	goto loop;
	return 1;

}
///////////////////
UINT FileSend(LPVOID lp)
{
	FileTransfer *p=(FileTransfer *)lp;
	
	//p->UpdateData(FALSE);
   
	//以内核态方式发送文件
	BOOL reat=false;
	::MessageBox(NULL,"文件发送线程开始工作!","1",MB_OK);
	char *buf;
	int len=0;
	unsigned long flen=0;
	flen=GetFileSize(p->m_hFiletosend,NULL);
	CString ss=_T("");
	ss.Format("文件长度:%d",flen);
    ::MessageBox(NULL,ss.GetBuffer(0),"2",MB_OK);

	CFile ccfile;
	ccfile.Open(p->m_filetobesent.GetBuffer(0),CFile::modeRead);
	ss=ccfile.GetFileName();
    ::MessageBox(NULL,ss.GetBuffer(0),"3",MB_OK);
	ccfile.Close();
	len=ss.GetLength();
	// ss.Format("文件名长度:%d",len);
	::MessageBox(NULL,ss.GetBuffer(0),"4",MB_OK);
	buf=new char[8+len];
    *((unsigned long*)buf)=flen;
	*((int *)(buf+4))=len;
    
	::MessageBox(NULL,"长度定位完毕!","5",MB_OK);
	strcpy((char *)(buf+8),ss.GetBuffer(0));
	send(p->m_hsock,buf,8+len,0);
	// Sleep(1000);
	reat=TransmitFile(p->m_hsock,	p->m_hFiletosend,
		       0,1024,NULL,NULL,0 );
	//WaitForSingleObject(hEvent,INFINITE);
	CloseHandle(p->m_hFiletosend);
    closesocket(p->m_hsock);
	if(reat)
	{
		::MessageBox(NULL,"文件发送成功!","",MB_OK);
	}
	else 
	{
     ::MessageBox(NULL,"文件发送失败!","",MB_OK);
	}
    delete []buf;
	::MessageBox(NULL,"发送线程将返回!","",MB_OK);
	return 0;    
}
/////////////////////////////////////////////////////////////////////////////
// CChatApp

BEGIN_MESSAGE_MAP(CChatApp, CWinApp)
	//{{AFX_MSG_MAP(CChatApp)
		// 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()

/////////////////////////////////////////////////////////////////////////////
// CChatApp construction

CChatApp::CChatApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CChatApp object

CChatApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CChatApp initialization

BOOL CChatApp::InitInstance()
{   
	WSADATA wsadata;
	if(WSAStartup(MAKEWORD(2,2),&wsadata))
	{
		AfxMessageBox("Windows Sockets Dll初始化失败!");
	}

	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

	CChatDlg 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;
}

int CChatApp::ExitInstance() 
{
	WSACleanup();
	return CWinApp::ExitInstance();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -