1. 程式人生 > >TCP文件發送

TCP文件發送

fifo log bind pac 文件名 輸入 count() filepath style

發送端(客戶端)

  1 #include <iostream>
  2 #include <winsock2.h>
  3 #include <Ws2tcpip.h>
  4 #include <fstream>
  5 using namespace std;
  6 
  7 #pragma comment(lib, "ws2_32.lib")
  8 
  9 struct FileHeader
 10 {
 11 
 12     char m_szFileName[MAX_PATH];
 13     int  m_nFileSize;
14 15 }; 16 17 int main() 18 { 19 WORD wVersionRequested; 20 WSADATA wsaData; 21 int err; 22 23 24 wVersionRequested = MAKEWORD(2, 2); 25 26 err = WSAStartup(wVersionRequested, &wsaData); 27 if (err != 0) { 28 29 printf("WSAStartup failed with error: %d\n
", err); 30 return 1; 31 } 32 33 34 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { 35 36 printf("Could not find a usable version of Winsock.dll\n"); 37 WSACleanup(); 38 return 1; 39 } 40 else 41 printf("
The Winsock 2.2 dll was found okay\n"); 42 43 SOCKET sockClient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 44 if(INVALID_SOCKET == sockClient) 45 { 46 WSACleanup(); 47 return 1; 48 } 49 cout<<"請輸入服務器iP"<<endl; 50 char szip[100] = {0}; 51 cin>>szip; 52 sockaddr_in addr; 53 54 55 addr.sin_addr.S_un.S_addr=inet_addr("192.168.202.1"); 56 57 addr.sin_family = AF_INET; 58 //addr.sin_addr.S_un.S_addr = inet_addr("192.168.1.251"); 59 addr.sin_port = htons(1234); 60 61 if(SOCKET_ERROR == connect(sockClient,(const sockaddr*)&addr,sizeof(addr))) 62 { 63 WSACleanup(); 64 closesocket(sockClient); 65 return 1; 66 } 67 68 char szFilePath[MAX_PATH] = {0}; 69 char *ptemp = szFilePath; 70 char szResult[4] = {0}; 71 char szContent[1024] = {0}; 72 //文件頭 73 //文件名,文件大小 74 FileHeader fh; 75 cout<<"請輸入要傳輸的文件"<<endl; 76 cin>>szFilePath; 77 78 //獲得文件名 79 while(*ptemp++ != \0); 80 while(*(--ptemp) != \\); 81 ptemp++; 82 83 strcpy_s(fh.m_szFileName,MAX_PATH,ptemp); 84 85 fstream fs; 86 fs.open(szFilePath,fstream::in|fstream::binary); 87 88 89 fs.seekg(0,fstream::end); 90 fh.m_nFileSize = fs.tellg(); //獲得文件指針所在位置的文件大小 91 fs.seekg(0,fstream::beg); 92 //發送 93 send(sockClient,(const char*)&fh,sizeof(fh),0); 94 95 //等待對方回復 96 recv(sockClient,szResult,sizeof(szResult),0); 97 98 if(0 == strcmp(szResult,"")) 99 { 100 //如果是肯定回復 101 102 while(1) 103 { 104 //讀文件 105 fs.read(szContent,sizeof(szContent)); 106 int nRelReadNum = fs.gcount(); 107 if(nRelReadNum == 0) 108 { 109 break; 110 } 111 //發文件 112 send(sockClient,szContent,nRelReadNum,0); 113 } 114 115 } 116 //關閉文件 117 fs.close(); 118 closesocket(sockClient); 119 WSACleanup(); 120 system("pause"); 121 return 0; 122 }

接收端(服務器)

  1 #include<stdio.h>
  2 #include<iostream>
  3 #include<fstream>
  4 #include<winsock2.h>
  5 #pragma comment(lib,"ws2_32.lib")
  6 using namespace std;
  7 struct fileFifo
  8 {
  9     char fileName[MAX_PATH];
 10     int fileSize;
 11 };
 12 int main()
 13 {
 14     //初始化wsa
 15     WORD wVersionRequested;
 16     WSADATA wsaData;
 17     int err;
 18     wVersionRequested = MAKEWORD(2, 2);
 19 
 20     err = WSAStartup(wVersionRequested, &wsaData);
 21     if (err != 0)
 22     {
 23                       
 24         printf("WSAStartup failed with error: %d\n", err);
 25         return 1;
 26     }
 27 
 28     if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) 
 29     {
 30        
 31         printf("Could not find a usable version of Winsock.dll\n");
 32         WSACleanup();
 33         return 1;
 34     }
 35     else
 36         printf("The Winsock 2.2 dll was found okay\n");
 37     
 38     SOCKET sever;
 39     SOCKET waiter;
 40     struct sockaddr_in severaddr;
 41     struct sockaddr_in clientaddr;
 42     char recvbuf[1024];//接受緩沖區
 43     char sendbuf[1024];//發送緩沖區
 44     //創建套接字
 45     sever=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
 46     if(sever==INVALID_SOCKET)
 47     {
 48         WSACleanup();
 49         return 1;
 50     }
 51     /*if(sever=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)==INVALID_SOCKET)
 52     {
 53         WSACleanup();
 54         return 1;
 55     }*/
 56     //綁定
 57     severaddr.sin_family=AF_INET;
 58     severaddr.sin_port=htons(1234);
 59     severaddr.sin_addr.S_un.S_addr=inet_addr("192.168.202.1");
 60     
 61     if(bind(sever,(const sockaddr*)&severaddr,sizeof(severaddr))==SOCKET_ERROR)
 62     {
 63         WSACleanup();
 64         closesocket(sever);
 65         return 1;
 66     }
 67     if(listen(sever,4)==SOCKET_ERROR)
 68     {
 69         WSACleanup();
 70         return 1;
 71     }
 72     printf("等待連接...\n");
 73     int clientlen=sizeof(clientaddr);
 74     waiter=accept(sever,(SOCKADDR*)&clientaddr,&clientlen);
 75     if(waiter==INVALID_SOCKET)//第一個參數為監聽套接字
 76     {
 77         printf("接受失敗\n");
 78         WSACleanup();
 79         closesocket(sever);
 80         return 1;
 81     }
 82     char filePath[MAX_PATH];
 83     char result[4]={0};
 84     char context[1024];
 85     fileFifo file;
 86     fstream fs;
 87     recv(waiter,(char *)&file,sizeof(file),0);
 88 
 89     cout<<"文件大小為:"<<file.fileSize<<endl;
 90     cout<<"請輸入是否接受該文件"<<endl;
 91     cin>>result;
 92     int tempsize=file.fileSize;
 93 
 94     send(waiter,result,sizeof(result),0);
 95 
 96     if(strcmp(result,"")==0)
 97     {
 98         cout<<"您要存儲得路徑為:"<<endl;
 99         cin>>filePath;
100         strcat_s(filePath,MAX_PATH,file.fileName);
101         //創建文件用於接受
102         fs.open(filePath,fstream::binary|fstream::out|fstream::trunc);
103         while(tempsize)
104         {
105             //接收文件內容
106             int numRecv=recv(waiter,context,sizeof(context),0);
107             //寫文件
108             if(numRecv>0)
109             {
110                 tempsize-=numRecv;
111                 fs.write(context,numRecv);
112             }
113         }
114     }
115     fs.close();
116     WSACleanup();
117     closesocket(sever);
118     closesocket(waiter);
119     system("pause");
120     return 0;
121 }

TCP文件發送