FtpServerDlg.cpp
资源名称:ftp_c_s.rar [点击查看]
上传用户:shjunxiao
上传日期:2022-01-25
资源大小:7105k
文件大小:9k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // FtpServerDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "FtpServer.h"
- #include "FtpServerDlg.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;
- CRITICAL_SECTION CriticalSection;//多线程同步用到的创建互斥对象需要的
- typedef struct tagSubPack
- {
- SOCKET socket;//套接字
- fd_set fdSocket;//套接字集,即SOCKET数组
- }SubPack;
- char szPath[256] = { 0 };//服务器端保存文件的路径
- 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()
- /////////////////////////////////////////////////////////////////////////////
- // CFtpServerDlg dialog
- CFtpServerDlg::CFtpServerDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CFtpServerDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CFtpServerDlg)
- m_port = _T("8888");
- m_mulu = _T("D:\ftp\");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CFtpServerDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CFtpServerDlg)
- DDX_Control(pDX, IDC_START, m_start);
- DDX_Text(pDX, IDC_EDIT_PORT, m_port);
- DDX_Text(pDX, IDC_EDIT_MULU, m_mulu);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CFtpServerDlg, CDialog)
- //{{AFX_MSG_MAP(CFtpServerDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_START, OnStart)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFtpServerDlg message handlers
- BOOL CFtpServerDlg::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
- m_sock = INVALID_SOCKET;
- strcpy(szPath,m_mulu);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CFtpServerDlg::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 CFtpServerDlg::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 CFtpServerDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- //启动服务
- void CFtpServerDlg::OnStart()
- {
- // TODO: Add your control notification handler code here
- InitializeCriticalSection(&CriticalSection);//多线程要用到的互斥对象创建,同步的
- //进入监听状态
- InitSocket();
- DWORD dwThread;
- HANDLE hThread = CreateThread(NULL, 0, RecvProc, (LPVOID)m_sock, 0, &dwThread);
- WaitForSingleObject(hThread, INFINITE);//INFINITE表示一直休息。。。直到新的线程请求
- DWORD ExitCode;
- GetExitCodeThread(hThread, &ExitCode);
- ExitThread(ExitCode);
- }
- //初始化套结字
- BOOL CFtpServerDlg::InitSocket()
- {
- if(m_sock == INVALID_SOCKET)
- {
- m_sock = socket(AF_INET,SOCK_STREAM ,0);
- }
- if(INVALID_SOCKET == m_sock)
- {
- MessageBox("创建套接字失败!");
- return FALSE;
- }
- CString sPort;
- GetDlgItem(IDC_EDIT_PORT)->GetWindowText(sPort);
- //MessageBox("开启的端口:"+sPort);
- int nPort = atoi(sPort);
- if(nPort < 1 || nPort > 65535)
- {
- MessageBox("端口号错误!");
- return FALSE;
- }
- SOCKADDR_IN srvAddr;
- srvAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
- srvAddr.sin_family = AF_INET;
- srvAddr.sin_port = htons(nPort);
- if(bind(m_sock,(SOCKADDR*)&srvAddr,sizeof(SOCKADDR))== SOCKET_ERROR)
- {
- closesocket(m_sock);
- MessageBox("绑定套接字失败!");
- return FALSE;
- }
- int m_nClient = listen(m_sock,5);
- if(m_nClient != 0)
- {
- MessageBox("Listen error!");
- closesocket(m_sock);
- return FALSE;
- }
- MessageBox("Success");
- return TRUE;
- }
- void CFtpServerDlg::CloseAllSocket()
- {
- // 关闭监听套节字
- if(m_sock != INVALID_SOCKET)
- {
- closesocket(m_sock);
- m_sock = INVALID_SOCKET;
- }
- }
- DWORD WINAPI CFtpServerDlg::SubRecvProc(LPVOID lpParameter)
- {
- SOCKET socket = ((SubPack *)lpParameter)->socket;
- DATA_PACKET recvPacket;
- char szTemp[24000] = { 0 };
- int nRecev=recv(socket, szTemp, //(char*)&recvPacket,
- 24000,0);
- int nPreIndex = 0;
- FILE *file = 0;
- if (nRecev > 0)
- {
- memcpy(&recvPacket, szTemp, sizeof(DATA_PACKET));
- char szFile[256] = { 0 };
- sprintf(szFile, "%s%s", szPath, recvPacket.szFileName);
- file = fopen( szFile, "wb" );
- }
- while(nRecev>0)
- {
- send(socket, (char*)"RECV_END",sizeof("RECV_END"),0 );
- for (unsigned int i = 0; i<nRecev/sizeof(DATA_PACKET); i++)
- {
- memset(&recvPacket, 0, sizeof(DATA_PACKET));
- memcpy(&recvPacket, szTemp + i * sizeof(DATA_PACKET), sizeof(DATA_PACKET));
- if (recvPacket.packetType == FILEPACKET)
- {
- fseek(file,(recvPacket.nIndex-1)*BUFSIZE,SEEK_SET );
- int errorcode=fwrite( recvPacket.content,1,recvPacket.length,file );
- fflush(file);
- }
- }
- memset(szTemp, 0, 24000);
- nRecev = recv(socket, szTemp, 24000,0);
- }
- if(file)
- fclose(file);
- return 0;
- }
- DWORD WINAPI CFtpServerDlg::RecvProc(LPVOID lpParameter)
- {
- SOCKET socket = (SOCKET)lpParameter;
- fd_set fdSocket;
- FD_ZERO(&fdSocket);
- FD_SET(socket,&fdSocket);
- timeval timeout;//这个结构体用于select函数,用于多线程中同步互斥时间间隔
- timeout.tv_sec=5;//5秒
- CRITICAL_SECTION CriticalSection;
- InitializeCriticalSection(&CriticalSection );
- while(true)
- {
- fd_set fdRead=fdSocket;
- int nRet=select(0,&fdRead,NULL,NULL,NULL);//Sockets select函数决定一个或多个socket的状态,
- //最后一个参数可以是timeout中具体设置的值,为空就直到一个请求到
- //来前一直等待。。。
- if(nRet>0)
- {
- for(int i=0;i<(int)fdSocket.fd_count;i++)
- { if(FD_ISSET(fdSocket.fd_array[i],&fdRead))
- {
- if(fdSocket.fd_array[i]==socket)
- {
- char szClientIP[128] = { 0 };
- sockaddr_in addrRemote;
- int nAddrLen=sizeof(addrRemote);
- SOCKET sNew=accept(socket,(sockaddr*)&addrRemote,&nAddrLen);//接收客户端的连接
- FD_SET(sNew,&fdSocket);//加入新的套接字进入套接字集
- sprintf( szClientIP, "%s",inet_ntoa(addrRemote.sin_addr) );
- }
- else
- {
- SubPack subpack;
- subpack.socket = fdSocket.fd_array[i];
- subpack.fdSocket = fdSocket;
- DWORD dwThread;
- HANDLE hThread = CreateThread(NULL, 0, SubRecvProc, (LPVOID)&subpack, 0, &dwThread);
- WaitForSingleObject(hThread, INFINITE);
- closesocket(fdSocket.fd_array[i]);
- FD_CLR(fdSocket.fd_array[i],&fdSocket);
- }
- }
- }
- }
- }
- return 0;
- }