P2P文件传输.cpp
上传用户:lj0533
上传日期:2022-08-08
资源大小:1392k
文件大小:12k
源码类别:

P2P编程

开发平台:

Visual C++

  1. // p2p.cpp : 定义应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "p2p.h"
  5. #include "winsock2.h"
  6. #pragma comment(lib,"ws2_32.lib")
  7. #include <Commdlg.h> 
  8. #define MAX_LOADSTRING 100
  9. #include"stdio.h"
  10. #include"windows.h"
  11. // 全局变量:
  12. HINSTANCE hInst; // 当前实例
  13. TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
  14. TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
  15. //// 此代码模块中包含的函数的前向声明:
  16. ATOM MyRegisterClass(HINSTANCE hInstance);
  17. BOOL InitInstance(HINSTANCE, int);
  18. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  19. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  20. int APIENTRY _tWinMain(HINSTANCE hInstance,
  21.                        HINSTANCE hPrevInstance,
  22.                        LPTSTR    lpCmdLine,
  23.                        int       nCmdShow)
  24. {
  25.     //UNREFERENCED_PARAMETER(hPrevInstance);
  26.     //UNREFERENCED_PARAMETER(lpCmdLine);
  27.     // // TODO: 在此放置代码。
  28.     //MSG msg;
  29.     //HACCEL hAccelTable;
  30.     //// 初始化全局字符串
  31.     //LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  32.     //LoadString(hInstance, IDC_P2P, szWindowClass, MAX_LOADSTRING);
  33.     //MyRegisterClass(hInstance);
  34.     //// 执行应用程序初始化:
  35.     //if (!InitInstance (hInstance, nCmdShow))
  36.     //{
  37.     // return FALSE;
  38.     //}
  39.     //hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_P2P));
  40.     //// 主消息循环:
  41.     //while (GetMessage(&msg, NULL, 0, 0))
  42.     //{
  43.     // if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  44.     // {
  45.     // TranslateMessage(&msg);
  46.     // DispatchMessage(&msg);
  47.     // }
  48.     //}
  49.     DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
  50.     //return (int) msg.wParam;
  51. }
  52. ////
  53. ////  函数: MyRegisterClass()
  54. ////
  55. ////  目的: 注册窗口类。
  56. ////
  57. ////  注释:
  58. ////
  59. ////    仅当希望
  60. ////    此代码与添加到 Windows 95 中的“RegisterClassEx”
  61. ////    函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
  62. ////    这样应用程序就可以获得关联的
  63. ////    “格式正确的”小图标。
  64. ////
  65. //ATOM MyRegisterClass(HINSTANCE hInstance)
  66. //{
  67. // WNDCLASSEX wcex;
  68. //
  69. // wcex.cbSize = sizeof(WNDCLASSEX);
  70. //
  71. // wcex.style = CS_HREDRAW | CS_VREDRAW;
  72. // wcex.lpfnWndProc = WndProc;
  73. // wcex.cbClsExtra = 0;
  74. // wcex.cbWndExtra = 0;
  75. // wcex.hInstance = hInstance;
  76. // wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_P2P));
  77. // wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  78. // wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  79. // wcex.lpszMenuName = MAKEINTRESOURCE(IDC_P2P);
  80. // wcex.lpszClassName = szWindowClass;
  81. // wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  82. //
  83. // return RegisterClassEx(&wcex);
  84. //}
  85. //
  86. ////
  87. ////   函数: InitInstance(HINSTANCE, int)
  88. ////
  89. ////   目的: 保存实例句柄并创建主窗口
  90. ////
  91. ////   注释:
  92. ////
  93. ////        在此函数中,我们在全局变量中保存实例句柄并
  94. ////        创建和显示主程序窗口。
  95. ////
  96. //BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  97. //{
  98. //   HWND hWnd;
  99. //
  100. //   hInst = hInstance; // 将实例句柄存储在全局变量中
  101. //
  102. //   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  103. //      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  104. //
  105. //   if (!hWnd)
  106. //   {
  107. //      return FALSE;
  108. //   }
  109. //
  110. //   ShowWindow(hWnd, nCmdShow);
  111. //   UpdateWindow(hWnd);
  112. //
  113. //   return TRUE;
  114. //}
  115. //
  116. ////
  117. ////  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
  118. ////
  119. ////  目的: 处理主窗口的消息。
  120. ////
  121. ////  WM_COMMAND - 处理应用程序菜单
  122. ////  WM_PAINT - 绘制主窗口
  123. ////  WM_DESTROY - 发送退出消息并返回
  124. ////
  125. ////
  126. //LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  127. //{
  128. // int wmId, wmEvent;
  129. // PAINTSTRUCT ps;
  130. // HDC hdc;
  131. //
  132. // switch (message)
  133. // {
  134. // case WM_COMMAND:
  135. // wmId    = LOWORD(wParam);
  136. // wmEvent = HIWORD(wParam);
  137. // // 分析菜单选择:
  138. // switch (wmId)
  139. // {
  140. // case IDM_ABOUT:
  141. // DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  142. // break;
  143. // case IDM_EXIT:
  144. // DestroyWindow(hWnd);
  145. // break;
  146. // default:
  147. // return DefWindowProc(hWnd, message, wParam, lParam);
  148. // }
  149. // break;
  150. // case WM_PAINT:
  151. // hdc = BeginPaint(hWnd, &ps);
  152. // // TODO: 在此添加任意绘图代码...
  153. // EndPaint(hWnd, &ps);
  154. // break;
  155. // case WM_DESTROY:
  156. // PostQuitMessage(0);
  157. // break;
  158. // default:
  159. // return DefWindowProc(hWnd, message, wParam, lParam);
  160. // }
  161. // return 0;
  162. //}
  163. unsigned short nPort;
  164. char szIP[15];
  165. WSADATA wsaData;
  166. char szBufRecv[1000];
  167. TCHAR szFile[1024]; // 保存获取文件名称的缓冲区。 ;
  168. sockaddr_in saSrv;
  169. SOCKET sRecv1;
  170. SOCKET sRecv2;
  171. SOCKET sSend1;
  172. SOCKET sSend2;
  173. char szBuf[1024];
  174. char filename[1024];
  175. // “关于”框的消息处理程序。
  176. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  177. {
  178.     UNREFERENCED_PARAMETER(lParam);
  179.     switch (message)
  180.     {
  181.     case WM_INITDIALOG:
  182.         SetDlgItemText(hDlg,IDC_EDIT_IP,"127.0.0.1");
  183.         //SetDlgItemText(hDlg,IDC_EDIT_PORT,"8888");
  184.         return (INT_PTR)TRUE;
  185.     case WM_COMMAND:
  186.         if(LOWORD(wParam)==IDC_RADIO1)
  187.             SendMessage(GetDlgItem(hDlg,IDC_RADIO1),BM_SETCHECK,BST_CHECKED,0);
  188.         if(LOWORD(wParam)==IDC_RADIO2)
  189.             SendMessage(GetDlgItem(hDlg,IDC_RADIO2),BM_SETCHECK,BST_CHECKED,0);
  190.         if(BST_CHECKED==SendMessage(GetDlgItem(hDlg,IDC_RADIO1),BM_GETCHECK,0,0))
  191.         {
  192.             if (LOWORD(wParam)==IDC_RADIO1)
  193.             {
  194.                 //初始化
  195.                 WSAStartup(MAKEWORD(2,2),&wsaData); //ws2_32.dll 
  196.                 //创建套接字
  197.                 sRecv1=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  198.                 //bind
  199.                 saSrv.sin_family =AF_INET;
  200.                 saSrv.sin_addr.S_un.S_addr =INADDR_ANY; //inet_addr("127.0.0.1");
  201.                 saSrv.sin_port =htons(7777);
  202.                 bind(sRecv1,(sockaddr*)&saSrv,sizeof(saSrv));
  203.     
  204.                 WSAStartup(MAKEWORD(2,2),&wsaData);
  205.                 //创建(接收)套接字
  206.                 sRecv2=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  207.                 sockaddr_in saSrv;
  208.                 saSrv.sin_family=AF_INET;
  209.                 saSrv.sin_addr.S_un.S_addr=INADDR_ANY;
  210.                 saSrv.sin_port=htons(8888);
  211.                 bind(sRecv2,(sockaddr*)&saSrv,sizeof(sockaddr_in));
  212.             }
  213.             if (LOWORD(wParam) == IDOK2)
  214.             {
  215.                 memset(filename,0,1024);
  216.                 recv(sRecv1,filename,1024,0);
  217.                 BOOL GetSaveFileName();
  218.                 {
  219.                     OPENFILENAME ofn;      // 公共对话框结构
  220.                     // 初始化选择文件对话框。
  221.                     ZeroMemory(&ofn, sizeof(ofn));
  222.                     ofn.lStructSize = sizeof(ofn);
  223.                     ofn.hwndOwner = NULL;
  224.                     ofn.lpstrFile = filename;
  225.                     ofn.nMaxFile = sizeof(filename);
  226.                     ofn.lpstrFilter = _T("All*.*Text*.TXT");
  227.                     ofn.nFilterIndex = 1;
  228.                     ofn.lpstrFileTitle =filename;
  229.                     ofn.nMaxFileTitle = 0;
  230.                     ofn.lpstrInitialDir = NULL;
  231.                     ofn.Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT;
  232.                     // 显示打开选择文件对话框。
  233.                     if ( GetSaveFileName(&ofn) )
  234.                     {
  235.                         //显示选择的文件。
  236.                         SetDlgItemText(hDlg,IDC_EDIT1,filename);
  237.                         return 0;
  238.                     }
  239.                 }
  240.                 shutdown(sRecv1,2);
  241.                 closesocket(sRecv1);
  242.                 WSACleanup();
  243.             }
  244.             if (LOWORD(wParam) == IDOK3)
  245.             {
  246.                 recv(sRecv2,filename,1024,0);
  247.                 FILE *fp; //定义文件类型的指针
  248.                 fp=fopen(filename,"wb");
  249.                 char buf[1024]; 
  250.                 memset(buf,0,1024);
  251.                 int nNumRead =1;
  252.                 while(nNumRead)
  253.                 {
  254.                     nNumRead = recv(sRecv2,buf,1024, 0 );
  255.                     fwrite(buf, 1, nNumRead, fp );
  256.                 }
  257.                 MessageBox(hDlg,"接收完成","",MB_OK);
  258.                 return FALSE;  
  259.                 fclose(fp);
  260.                 shutdown( sRecv2,2);
  261.                 closesocket(sRecv2);
  262.                 WSACleanup();
  263.             }      
  264.         }
  265.         if(BST_CHECKED==SendMessage(GetDlgItem(hDlg,IDC_RADIO2),BM_GETCHECK,0,0))
  266.         {
  267.             if(LOWORD(wParam)==IDOK2)
  268.             { 
  269.                 //初始化
  270.     WSAStartup(MAKEWORD(2,2),&wsaData);
  271.                 //创建套接字
  272.                 sSend1=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  273.                 if(sSend1==INVALID_SOCKET)
  274.                 {
  275.                     MessageBox(hDlg,"创建套接字失败","",MB_OK);
  276.                     break;
  277.                 }
  278.                 sockaddr_in saDst;
  279.                 saDst.sin_family=AF_INET;
  280.                 WORD nPort;
  281.                 GetDlgItemText(hDlg,IDC_EDIT_IP,szIP,15);
  282.                 //GetDlgItemText(hDlg,IDC_EDIT2,szPort,5);
  283.                 saDst.sin_addr.S_un.S_addr=inet_addr(szIP);
  284.                 saDst.sin_port=htons(7777);  
  285.                 BOOL GetOpenFileName();
  286.                 {
  287.                     OPENFILENAME ofn;      // 公共对话框结构。         
  288.                     // 初始化选择文件对话框。
  289.                     ZeroMemory(&ofn, sizeof(ofn));
  290.                     ofn.lStructSize = sizeof(ofn);
  291.                     ofn.hwndOwner =NULL;
  292.                     ofn.lpstrFile = szFile;
  293.                     ofn.lpstrFile[0] = _T('');
  294.                     ofn.nMaxFile = sizeof(szFile);
  295.                     ofn.lpstrFilter = _T("All*.*Text*.TXT");
  296.                     ofn.nFilterIndex = 1;
  297.                     ofn.lpstrFileTitle = NULL;
  298.                     ofn.nMaxFileTitle = 0;
  299.                     ofn.lpstrInitialDir = NULL;
  300.                     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  301.                     // 显示打开选择文件对话框。
  302.                     if ( GetOpenFileName(&ofn) )
  303.                     {
  304.                         //显示选择的文件。 
  305.                         SetDlgItemText(hDlg,IDC_EDIT1,szFile);
  306.                         GetDlgItemText(hDlg,IDC_EDIT1,szFile,1024);
  307.                         sendto(sSend1,szFile,strlen(szFile),0,(sockaddr*)&saDst,sizeof(saDst));
  308.                         //shutdown(sSend1, 2);
  309.                         //closesocket(sSend1);
  310.                         //WSACleanup();
  311.                     }
  312.                 }
  313.             }
  314.             if (LOWORD(wParam) == IDOK)
  315.             {
  316.                 WSAStartup(MAKEWORD(2,2),&wsaData);
  317.                 //创建套接字
  318.                 sSend2=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  319.                 if(sSend2==INVALID_SOCKET)
  320.                 {
  321.                     MessageBox(hDlg,"创建套接字失败","",MB_OK);
  322.                     break;
  323.                 }
  324.                 sockaddr_in saDst;
  325.                 saDst.sin_family=AF_INET;
  326.                 WORD nPort;
  327.                 GetDlgItemText(hDlg,IDC_EDIT_IP,szIP,15);
  328.                 saDst.sin_addr.S_un.S_addr=inet_addr(szIP);
  329.                 saDst.sin_port=htons(8888);  
  330.                 GetDlgItemText(hDlg,IDC_EDIT1,szFile,1024);
  331.                 FILE *fp; //定义文件类型的指针
  332.                 char filename[1024];
  333.                 strcpy(filename,szFile);
  334.                 fp=fopen(filename,"rb");
  335.                 if(fp==NULL)
  336.                 {
  337.                     MessageBox(hDlg,"文件打开失败","",MB_OK);
  338.                     return TRUE;
  339.                 }
  340.                 char buf[1024]; 
  341.                 memset(buf,0,1024);
  342.                 int nNumRead = 0;
  343.                 while( !feof( fp) )
  344.                 {
  345.                     nNumRead = fread( buf, 1, 1024, fp );
  346.                     int nCount=sendto(sSend2,buf,strlen(buf),0,(sockaddr*)&saDst,sizeof(saDst));
  347.                     if(nCount==SOCKET_ERROR)
  348.                     {
  349.                         MessageBox(hDlg,"发送数据失败","",MB_OK);
  350.                         return FALSE;
  351.                     }
  352.                 }
  353.                 fclose(fp);
  354.                 shutdown(sSend2, 2 );
  355.                 closesocket(sSend2);
  356.                 WSACleanup();
  357.                 MessageBox(hDlg,"发送完成","",MB_OK);
  358.                 return FALSE;
  359.             }
  360.         }
  361.         if (LOWORD(wParam) == IDCANCEL)
  362.         {
  363.             EndDialog(hDlg, LOWORD(wParam));
  364.             return (INT_PTR)TRUE;
  365.         }
  366.         break;
  367.     }
  368.     return (INT_PTR)FALSE;
  369. }