zoukankan      html  css  js  c++  java
  • 106.TCP传文件

    • 客户端
       1 #define  _CRT_SECURE_NO_WARNINGS
       2 #include <stdio.h>
       3 #include <stdlib.h>
       4 #include <winsock.h>
       5 #include <string.h>
       6 #pragma  comment(lib,"ws2_32.lib")
       7 
       8 //定义端口
       9 #define PORT 6666
      10 //缓冲区大小
      11 #define BUF_SIZE 1024
      12 //文件名大小
      13 #define FILENAMESIZE  512
      14 //本地地址
      15 #define SIP "192.168.1.108"
      16 
      17 //主函数
      18 void main()
      19 {
      20     //用于初始化网络
      21     WSADATA wsadata;
      22     //定义版本
      23     WORD socketversion = MAKEWORD(2, 2);
      24     //初始化网络
      25     if (WSAStartup(socketversion,&wsadata)!=0)
      26     {
      27         printf("网络初始化失败");
      28         return;
      29     }
      30     //创建TCP通信
      31     SOCKET  client_socket = socket(AF_INET, SOCK_STREAM, 0);
      32     //判断是否创建成功
      33     if (client_socket==SOCKET_ERROR)
      34     {
      35         printf("网络创建通信socket出错");
      36         return;
      37     }
      38     //设定服务器信息
      39     SOCKADDR_IN sever_addr;
      40     sever_addr.sin_family = AF_INET;
      41     sever_addr.sin_addr.S_un.S_addr = inet_addr(SIP);
      42     //设定IP,端口
      43     sever_addr.sin_port = htons(PORT);
      44     //连接到服务器
      45     if (connect(client_socket, (SOCKADDR*)&sever_addr, sizeof(sever_addr)) == SOCKET_ERROR)
      46     {
      47         puts("连接错误");
      48         return;
      49     }
      50     char filename[512] = { 0 };
      51     //初始化
      52     scanf("%s", filename);
      53 
      54     char buffer[1024] = { 0 };
      55     memset(buffer, 0, 1024);
      56     strcpy(buffer, filename);
      57 
      58     //传递文件名,请求下载
      59     if (send(client_socket,buffer,1024,0) <0)//传输出错
      60     {
      61         puts("传输错误");
      62         return;
      63     }
      64 
      65     //写文件
      66     FILE *pf= fopen(filename, "wb");
      67     if (pf==NULL)
      68     {
      69         puts("文件打开失败");
      70         return;
      71 
      72 
      73     }
      74     else
      75     {
      76         memset(buffer, 0, 1024);
      77         int length = 0;
      78         printf("文件开始传输");
      79         //不断接收
      80         while ((length=recv(client_socket,buffer,1024,0))>0)
      81         {
      82             if (fwrite(buffer,sizeof(char),length,pf)<length)
      83             {
      84                 puts("文件传输错误");
      85             }
      86             memset(buffer, 0, 1024);
      87         }
      88         printf("文件传输成功");
      89         system("pause");
      90         system(filename);
      91     }
      92     //关闭文件网络,清理网络
      93     fclose(pf);
      94     closesocket(client_socket);
      95     WSACleanup();
      96 }
    • 服务器端
        1 #define  _CRT_SECURE_NO_WARNINGS
        2 #include <stdio.h>
        3 #include <stdlib.h>
        4 #include <winsock.h>
        5 #include <string.h>
        6 #pragma  comment(lib,"ws2_32.lib")
        7 
        8 #define PORT 6666
        9 #define BUF_SIZE 1024
       10 #define FILENAMESIZE  512
       11 #define SIP "169.254.29.232"
       12 
       13 void main()
       14 {
       15     //初始化服务器ip地址与端口号并定义谁可以连接
       16     SOCKADDR_IN server_addr;
       17     server_addr.sin_family = AF_INET;
       18     server_addr.sin_port = htons(PORT);
       19     //本地ip地址
       20     server_addr.sin_addr.S_un.S_addr = INADDR_ANY;
       21     WSADATA wsadata;
       22     WORD socketversion = MAKEWORD(2, 2);//定义版本
       23     if (WSAStartup(socketversion, &wsadata) != 0)
       24     {
       25         printf("网络初始化失败");
       26         return;
       27     }
       28     //创建
       29     SOCKET server_socket = socket(AF_INET, SOCK_STREAM, 0);//创建
       30     if (server_socket == SOCKET_ERROR)
       31     {
       32         printf("网络创建通信socket出错");
       33         return;
       34     }
       35     //绑定
       36     if (SOCKET_ERROR == bind(server_socket,(SOCKADDR*) &server_addr, sizeof(server_addr)))
       37     {
       38         printf("bind 出错");
       39         return;
       40     }
       41     //监听
       42     if (SOCKET_ERROR == listen(server_socket,10))
       43     {
       44         printf("listen 出错");
       45         return;
       46     }
       47     //传文件
       48 
       49     while (1)
       50     {
       51         printf("进入listen,开始等待客户端");
       52         //客户端地址
       53         SOCKADDR_IN client_addr;
       54         int clientlength = sizeof(client_addr);
       55         //接收连接
       56         SOCKET new_socket = accept(server_socket, (SOCKADDR*)&client_addr, &clientlength);
       57         if (SOCKET_ERROR == new_socket)
       58         {
       59             printf("服务器已经满或者链接失败");
       60             break;
       61         }
       62         char buffer[1024] = { 0 };
       63         char filename[512] = { 0 };
       64         //接收信息
       65         if (recv(new_socket,buffer,1024,0)<0)
       66         {
       67             printf("收取文件名失败");
       68             break;
       69         }
       70         strcpy(filename, buffer);
       71         
       72         //以二进制方式读取
       73         FILE *pf = fopen(filename, "rb");
       74         if (pf == NULL)
       75         {
       76             printf("文件打开失败");
       77             break;
       78         } 
       79         else
       80         {
       81             memset(buffer, 0, 1024);//清零
       82             int recelength = 0;
       83             while ((recelength = fread(buffer, sizeof(char), 1024, pf)>0))
       84             {
       85                 if (send(new_socket,buffer,1024,0)<0)
       86                 {
       87                     printf("文件传输失败");
       88                     break;
       89                 }
       90                 memset(buffer, 0, 1024);//清零
       91             }
       92             fclose(pf);
       93             printf("传输完成");
       94 
       95         }
       96 
       97         closesocket(new_socket);
       98     }
       99     closesocket(server_socket);
      100     system("pause");
      101     WSACleanup();
      102 }
  • 相关阅读:
    c语言中的数据变量类型,大小
    表达式* ptr ++和++ * ptr是否相同?
    再论i++ ++i
    Chapter 1 First Sight——2
    如何修改博客样式
    PAT1011
    Chapter 1 First Sight——1
    Preface
    L11,one good turn deserves another
    PAT1010
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8494910.html
Copyright © 2011-2022 走看看