www.pudn.com > RemoteFileOpe.rar > FileOperateServerDlg.cpp


                 // FileOperateServerDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "FileOperateServer.h" 
#include "FileOperateServerDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFileOperateServerDlg dialog 
 
CFileOperateServerDlg::CFileOperateServerDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CFileOperateServerDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CFileOperateServerDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CFileOperateServerDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CFileOperateServerDlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CFileOperateServerDlg, CDialog) 
	//{{AFX_MSG_MAP(CFileOperateServerDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BSTART, OnBstart) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFileOperateServerDlg message handlers 
 
BOOL CFileOperateServerDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CFileOperateServerDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CFileOperateServerDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CFileOperateServerDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
CString GetFileName(char buffer[100]) 
{ 
    char fileName[100]; 
    memcpy(fileName,buffer,100); 
	return fileName; 
} 
 
UINT WorkThread(LPVOID p) 
{ 
	CString               sReceivedString, sSendString; 
	UINT                  ServPort = 3456; 
	CSocket               ServSocket,AcceptSocket; 
	char                  buffer[100] = "\0"; 
	 
	CFileOperateServerDlg *dlg=(CFileOperateServerDlg*)AfxGetApp()->GetMainWnd(); 
	 
  
	if(ServSocket.Create(ServPort,SOCK_STREAM,NULL)) 
		dlg->SetDlgItemText(IDC_STATIC_STATE, "Socket 成功创建"); 
	 
	ServSocket.Listen(); 
	ServSocket.Accept(AcceptSocket,NULL,NULL); 
 
    dlg->SetDlgItemText(IDC_STATIC_STATE, "控制端连接上"); 
	int response =1; 
	while(response != INVALID_SOCKET) 
	{ 
		 
		response = AcceptSocket.Receive(buffer,sizeof(buffer),0); 
	//	dlg->SetDlgItemText(IDC_STATIC_STATE, buffer); 
//		AcceptSocket.Send("收到数据",100,0); 
		if(buffer[0] == '$') 
		{ 
			dlg->m_FileName = GetFileName(buffer);	 
			dlg->m_FileName = dlg->m_FileName.Right( dlg->m_FileName.GetLength() - 1 ); 
			CString str = "接收到要操作的文件名称:" + dlg->m_FileName; 
			dlg->SetDlgItemText(IDC_STATIC_STATE, str); 
		} 
		if(buffer[0] == '#') 
		{ 
			switch(buffer[1]) 
			{ 
			case 'C':			 
				sSendString = dlg->FileOperate(6);//Create	 
				AcceptSocket.Send(sSendString,100,0); 
				dlg->SetDlgItemText(IDC_STATIC_STATE, "送出创建信息"); 
				break; 
			case 'D': 
				sSendString = dlg->FileOperate(3);//Delete 
				AcceptSocket.Send(sSendString,100,0); 
				dlg->SetDlgItemText(IDC_STATIC_STATE, "送出删除信息"); 
				break; 
			case 'M': 
				sSendString = dlg->FileOperate(4);//Move 
				AcceptSocket.Send(sSendString,100,0); 
				dlg->SetDlgItemText(IDC_STATIC_STATE, "送出移动信息"); 
				break; 
			case 'R': 
				sSendString = dlg->FileOperate(5);//Rename 
				AcceptSocket.Send(sSendString,100,0); 
				dlg->SetDlgItemText(IDC_STATIC_STATE, "送出重命名信息"); 
				break; 
			case 'F': 
				sSendString = dlg->FileOperate(1);//Copy 
				AcceptSocket.Send(sSendString,100,0); 
				dlg->SetDlgItemText(IDC_STATIC_STATE, "送出拷贝信息"); 
				break; 
			case 'A': 
				sSendString = dlg->FileOperate(7);//Access 
				AcceptSocket.Send(sSendString,100,0); 
				dlg->SetDlgItemText(IDC_STATIC_STATE, "送出访问信息"); 
				break; 
			default: break; 
			} 
		} 
		 
	} 
	dlg->SetDlgItemText(IDC_STATIC_STATE, "控制端断开"); 
	dlg->GetDlgItem(IDC_BSTART)->EnableWindow(true); 
	ServSocket.Close(); 
	AfxEndThread(0); 
	return 1; 
} 
 
 
void CFileOperateServerDlg::OnBstart()  
{ 
	// TODO: Add your control notification handler code here 
	 
	GetDlgItem(IDC_BSTART)->EnableWindow(false); 
	AfxBeginThread(WorkThread, 0); 
	 
} 
 
CString CFileOperateServerDlg::FileOperate(int Com) 
{ 
    CString  from, to,location; 
    from = m_FileName; 
	to = "Copy.txt"; 
	CStdioFile file; 
	switch(Com) 
	{ 
	case 1: 
		if(CopyFile(from, to, TRUE)) 
			return "拷贝成功 Copy.txt"; 
		else return "拷贝失败,可能是文件已存在"; 
		break; 
 
   case 3: 
		if(DeleteFile(m_FileName)) 
		{ 
			CString str = "已成功删除文件:"; 
			str += m_FileName; 
			return str; 
		} 
		else return "文件删除失败"; 
		break; 
 
	case 4: 
	     location = "..\\"+  m_FileName; 
		if(MoveFileEx(to, location, MOVEFILE_COPY_ALLOWED)) 
		{	 
		    CString str = "文件移动到:"+location; 
			return str; 
		} 
		else return "移动失败,已经存在"; 
		break; 
	case 5: 
		TRY 
		{ 
			CFile::Rename( to, "Rename.txt" ); 
			CString str = "文件已重命名为:Rename.txt"; 
			 
			return str; 
		} 
		CATCH( CFileException, e ) 
		{ 
			if( e->m_cause == CFileException::fileNotFound ) 
			return "重命名失败"; 
		} 
		END_CATCH 
		break;	 
	case 6: //创建文件 
		if(file.Open(m_FileName, CFile::modeCreate)) 
		{ 
			return "文件创建成功"; 
			file.Close(); 
		} 
		else return "文件创建失败,可能已存在"; 
		break; 
	case 7: //访问文件 
//		CStdioFile file2; 
		if(file.Open( m_FileName, CFile::modeRead | CFile::typeText)) 
		{ 
			CString te; 
			file.ReadString(te); 
			return te; 
			file.Close(); 
		} 
			else return "文件打开失败,可能不存在"; 
		break; 
	default: 
		file.Close(); 
	} 
}