• TCP文件发送


    发送端(客户端)

      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];//windows下,全文件名的字符长度是有限制的,众所周知,是MAX_PATH,260
     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
    ", 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
    ");
     37         WSACleanup();
     38         return 1;
     39     }
     40     else
     41         printf("The Winsock 2.2 dll was found okay
    ");
     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++ != '');
     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);//将文件指针偏移位置置回0
     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
    ", 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
    ");
     32         WSACleanup();
     33         return 1;
     34     }
     35     else
     36         printf("The Winsock 2.2 dll was found okay
    ");
     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("等待连接...
    ");
     73     int clientlen=sizeof(clientaddr);
     74     waiter=accept(sever,(SOCKADDR*)&clientaddr,&clientlen);
     75     if(waiter==INVALID_SOCKET)//第一个参数为监听套接字
     76     {
     77         printf("接受失败
    ");
     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 }
  • 相关阅读:
    vue.js 第二课
    vue.js学习(第一课)
    2016-11-14看张大神的微博总结
    这几天的工作总结:
    调了一天的兼容总结下
    鸭式辩论
    prototype 原型
    前端ps常用的小技巧
    Android的开始之相对布局
    Android的开始之线性布局
  • 原文地址:https://www.cnblogs.com/curo0119/p/8378099.html
Copyright © 2020-2023  润新知