FtpClientDlg.cpp
资源名称:ftp_c_s.rar [点击查看]
上传用户:shjunxiao
上传日期:2022-01-25
资源大小:7105k
文件大小:8k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // FtpClientDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "FtpClient.h"
- #include "FtpClientDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- static int BUFSIZE = 2000;//文件分块的大小,以字节算
- typedef enum {FILEPACKET}PacketType;
- //文件分块的结构
- typedef struct{
- PacketType packetType;//块类型:文件块
- char szFileName[256];//文件名
- long nIndex;//文件块的序号
- long nCount;//文件分块的个数
- char content[2001];//存储单个文件块内容
- unsigned int length;//单个文件块内容的长度
- }DATA_PACKET;
- long GetFileLength(char *strPath)
- {//得到文件的长度,字节数
- long retLength;
- FILE *stream;
- if( (stream = fopen( strPath, "r" )) == NULL )
- return 0;
- fseek( stream, 0, SEEK_END);
- retLength=ftell( stream );
- fclose( stream );
- return retLength;
- }
- 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()
- /////////////////////////////////////////////////////////////////////////////
- // CFtpClientDlg dialog
- CFtpClientDlg::CFtpClientDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CFtpClientDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CFtpClientDlg)
- m_port = _T("8888");
- m_IP = _T("127.0.0.1");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CFtpClientDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CFtpClientDlg)
- DDX_Control(pDX, IDC_PUT_FILE, m_put_file);
- DDX_Control(pDX, IDC_CONNECT, m_connect);
- DDX_Text(pDX, IDC_EDIT_PORT, m_port);
- DDX_Text(pDX, IDC_EDIT_IP, m_IP);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CFtpClientDlg, CDialog)
- //{{AFX_MSG_MAP(CFtpClientDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_CONNECT, OnConnect)
- ON_BN_CLICKED(IDC_PUT_FILE, OnPutFile)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFtpClientDlg message handlers
- BOOL CFtpClientDlg::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 CFtpClientDlg::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 CFtpClientDlg::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 CFtpClientDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- //连接到服务器的函数
- void CFtpClientDlg::OnConnect()
- {
- // TODO: Add your control notification handler code here
- char buff[20];
- strcpy(buff,m_IP);
- //MessageBox(buff);
- //获取端口号的值
- int port = atoi(m_port);
- m_sock = socket(AF_INET,SOCK_STREAM ,0);
- if(INVALID_SOCKET == m_sock)
- {
- return ;
- }
- SOCKADDR_IN srvAddr;
- srvAddr.sin_addr.S_un.S_addr = inet_addr(buff);
- srvAddr.sin_family = AF_INET;
- srvAddr.sin_port = htons(port);
- if(connect(m_sock, (sockaddr*)&srvAddr, sizeof(srvAddr))== -1)
- {
- MessageBox("连接失败");
- return ;
- }
- else
- MessageBox("连接服务器成功");
- m_connect.EnableWindow(false);
- }
- //上传文件到服务器
- void CFtpClientDlg::OnPutFile()
- {
- // TODO: Add your control notification handler code here
- CFileDialog fileDlg(TRUE,NULL,NULL);
- if(fileDlg.DoModal() == IDCANCEL)
- return;
- CString filePath; //文件路径
- filePath = fileDlg.GetPathName();
- char szFile[256] = { 0 };
- strcpy(szFile,filePath);
- char* pSend=new char[BUFSIZE];
- long fLength = GetFileLength(szFile); //文件的字节数
- CRITICAL_SECTION CriticalSection;
- InitializeCriticalSection(&CriticalSection );//多线程要用到的互斥,同步的
- long segLength = fLength;//文件的字节数
- long sendFileLen = 0;
- FILE *file;
- if( (file = fopen( szFile, "rb" )) == NULL )
- {
- MessageBox("faild to open file");
- return;
- }
- char szFileName[128] = { 0 };
- sprintf(szFileName, "%s", strrchr(szFile, '\'));
- strcpy(szFileName, szFileName+1);
- int nCount;//文件的分块总数
- if (fLength % BUFSIZE)//fLength为文件的字节数,BUFSIZE为每次文件分块的字节数
- {
- nCount = fLength / BUFSIZE + 1;
- }
- else
- {
- nCount = fLength / BUFSIZE;
- }
- int packIndex=1;//文件块的序号,也用来表示文件块传了多少了
- while(packIndex <= nCount) // send file
- {
- DATA_PACKET packSend;
- memset(&packSend, 0, sizeof(DATA_PACKET));//设定大小
- packSend.packetType = FILEPACKET ;//块的类型为文件块
- strcpy(packSend.szFileName, szFileName);
- packSend.nIndex = packIndex;
- packSend.nCount = nCount;
- memset(pSend, 0, BUFSIZE);
- fseek(file,(packIndex-1)*BUFSIZE,SEEK_SET);//将文件指针移到特定位置
- if( segLength < BUFSIZE )
- {
- fread( pSend, sizeof(char), segLength, file ); //从文件数据流中读取数据
- packSend.length = segLength;
- memcpy( packSend.content, pSend, segLength );
- send( m_sock,(char*)&packSend,sizeof(packSend),0 );//向服务器发送文件块信息
- break;
- }
- else
- {
- fread( pSend, sizeof(char), BUFSIZE, file );
- memcpy( packSend.content, pSend, BUFSIZE );
- packSend.length = BUFSIZE;
- send( m_sock,(char*)&packSend,sizeof(packSend),0 );//向服务器发送文件块信息
- char szRecv[128] = { 0 };
- while (true)
- {
- int nrecv = recv(m_sock, szRecv, 128,0);
- if (strcmp(szRecv, "RECV_END") == 0)
- {
- //MessageBox("recv ok!");
- break;
- }
- }
- }
- packIndex++;
- }
- //MessageBox("send end");
- fclose(file);
- delete []pSend;
- closesocket(m_sock);
- m_put_file.EnableWindow(false);
- return;
- }