IO重叠结构发送端代码

    技术2022-05-11  91

    // client.cpp : Defines the class behaviors for the application.//

    #include "stdafx.h"#include "winsock2.h"#include "client.h"#include "clientDlg.h"

    #ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif

    /// CClientApp

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

    /// CClientApp construction

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

    /// The one and only CClientApp object

    CClientApp theApp;

    /// CClientApp initialization

    BOOL CClientApp::InitInstance(){ 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

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


     // client.h : main header file for the CLIENT application //

    #if !defined(AFX_CLIENT_H__C825687A_20FA_4894_BB0D_991130B29BAB__INCLUDED_)#define AFX_CLIENT_H__C825687A_20FA_4894_BB0D_991130B29BAB__INCLUDED_

    #if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000

    #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH#endif

    #include "resource.h"  // main symbols

    /// CClientApp:// See client.cpp for the implementation of this class//

    class CClientApp : public CWinApp{public: CClientApp();

    // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CClientApp) public: virtual BOOL InitInstance(); //}}AFX_VIRTUAL

    // Implementation

     //{{AFX_MSG(CClientApp)  // NOTE - the ClassWizard will add and remove member functions here.  //    DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP()};

    /

    //{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

    #endif // !defined(AFX_CLIENT_H__C825687A_20FA_4894_BB0D_991130B29BAB__INCLUDED_)


    // clientDlg.cpp : implementation file//

    #include "stdafx.h"#include "iostream"#include "winsock2.h"#include "mswsock.h"#include "client.h"#include "clientDlg.h"

    #ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif

    /// CAboutDlg dialog used for App About

    UINT SStreadproc(LPVOID lParam); //发送线程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

    // Implementationprotected: //{{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_MAPEND_MESSAGE_MAP()

    /// CClientDlg dialog

    CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/) : CDialog(CClientDlg::IDD, pParent){ //{{AFX_DATA_INIT(CClientDlg)  // 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 CClientDlg::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CClientDlg)  // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP}

    BEGIN_MESSAGE_MAP(CClientDlg, CDialog) //{{AFX_MSG_MAP(CClientDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON2, OnButton2) //}}AFX_MSG_MAPEND_MESSAGE_MAP()

    /// CClientDlg message handlers

    BOOL CClientDlg::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 WSADATA wsadata; WSAStartup(MAKEWORD(2,2),&wsadata); ZeroMemory(&SSoverlapped,sizeof(WSAOVERLAPPED)); SSeventnums=0; Socket=WSASocket(AF_INET,SOCK_STREAM,0,NULL,0,WSA_FLAG_OVERLAPPED); return TRUE;  // return TRUE  unless you set the focus to a control}

    void CClientDlg::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 CClientDlg::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 CClientDlg::OnQueryDragIcon(){ return (HCURSOR) m_hIcon;}

    void CClientDlg::OnButton1() { // TODO: Add your control notification handler code here SOCKADDR_IN sockaddr; sockaddr.sin_family=AF_INET; sockaddr.sin_addr.s_addr=inet_addr("127.0.0.1"); sockaddr.sin_port=htons(8589); if(connect(Socket,(PSOCKADDR)&sockaddr,sizeof(sockaddr))!=SOCKET_ERROR)  AfxMessageBox("连接服务器成功");}

    void CClientDlg::OnButton2() { // TODO: 发送数据的函数 char strdir[MAX_PATH]; char strpath[MAX_PATH]; BROWSEINFO bi={0}; bi.hwndOwner=this->GetSafeHwnd(); bi.pidlRoot=NULL; bi.lpszTitle="请选择你要打开的路径"; bi.ulFlags=BIF_RETURNONLYFSDIRS|BIF_EDITBOX; bi.pszDisplayName=strdir; LPITEMIDLIST pidl=SHBrowseForFolder(&bi); if (SHGetPathFromIDList(pidl,strpath)) {  CString filepath;  filepath.Format("%s",strpath);    //得到要发送文件夹的路径  filepath=filepath+"//*.*";  CFileFind fileinfo;  BOOL bcontinue=fileinfo.FindFile(filepath);  while(bcontinue)  {   bcontinue=fileinfo.FindNextFile();   if(fileinfo.IsDirectory()!=TRUE&&fileinfo.IsDots()!=TRUE)   {    CString filename,strsize;    char Finfo[512];    CString str;    str=fileinfo.GetFilePath();    hfile=CreateFile(str,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);    filename=fileinfo.GetFileName();    int size=fileinfo.GetLength();    strsize.Format("%d",size);    filename=filename+"|"+strsize;    memcpy(Finfo,filename,filename.GetLength()+1);    WSABUF wsabuf;    DWORD flags=0;    DWORD nums;    wsabuf.len=filename.GetLength()+1;    wsabuf.buf=Finfo;    WSASend(Socket,&wsabuf,1,&nums,0,NULL,NULL);    AfxBeginThread(SStreadproc,NULL);    recv(Socket,Finfo,512,0);   }  } } }

    UINT SStreadproc(LPVOID lParam){

     CClientDlg *app=(CClientDlg*)AfxGetApp()->GetMainWnd(); TransmitFile(app->Socket,app->hfile,0,4096,NULL,NULL,0); return 0; }


    // clientDlg.h : header file//

    #if !defined(AFX_CLIENTDLG_H__5E3CB425_D3D0_47D2_ACD6_DC3A70427604__INCLUDED_)#define AFX_CLIENTDLG_H__5E3CB425_D3D0_47D2_ACD6_DC3A70427604__INCLUDED_

    #if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000

    /// CClientDlg dialog

    class CClientDlg : public CDialog{// Constructionpublic: HANDLE hfile; WSAOVERLAPPED SSoverlapped; WSAEVENT SSeventarray[WSA_MAXIMUM_WAIT_EVENTS]; DWORD SSeventnums; SOCKET Socket; CClientDlg(CWnd* pParent = NULL); // standard constructor

    // Dialog Data //{{AFX_DATA(CClientDlg) enum { IDD = IDD_CLIENT_DIALOG };  // NOTE: the ClassWizard will add data members here //}}AFX_DATA

     // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CClientDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL

    // Implementationprotected: HICON m_hIcon;

     // Generated message map functions //{{AFX_MSG(CClientDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnButton1(); afx_msg void OnButton2(); //}}AFX_MSG DECLARE_MESSAGE_MAP()};

    //{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

    #endif // !defined(AFX_CLIENTDLG_H__5E3CB425_D3D0_47D2_ACD6_DC3A70427604__INCLUDED_)


    最新回复(0)