[X]关闭

网络编程_3day_tcp传输文件

文档创建者:ぉ沙皮狗的忧伤
浏览次数:5235
最后更新:2021-03-10
本帖最后由 ぉ沙皮狗的忧伤 于 2021-3-10 18:22 编辑

一、sever端代码编写代码编写流程
        ①、添加头文件
        ②、宏定义端口及ip
        ③、建立套接字
        ④、绑定本地IP和端口
        ⑤、设置监听模式,套接字变为被动等待连接
        ⑥、等待客户端连接
        ⑦、打开文件
        ⑧、接收数据保存到文件中
1、添加头文件

  1. //添加头文件#
  2. #include <string.h>
  3. #include <strings.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <arpa/inet.h>
  11. #include <sys/socket.h>
  12. #include <netinet/ip.h>
  13. #include <netinet/in.h>
  14. #include <arpa/inet.h>
复制代码



2、宏定义端口及IP
  1. #define SER_PORT           9999
  2. #define SER_IP            "172.18.21.163"
复制代码
实现sever端代码
3、建立流失套接字
  1.         //建立流失套接字
  2.         //参数1----地址族,AF_INET网络地址族
  3.         //参数2----套接字类型,SOCK_STREAM使用TCP
  4.         //参数3----protocol通常设置为0
  5.         //返回值---成功返回socket id 失败返回-1
  6.         serfd = socket( AF_INET, SOCK_STREAM, 0);
  7.         if(serfd < 0){
  8.                 perror("socket failed!\n");
  9.                 exit(1);
  10.         }
复制代码
4、绑定本地IP和端口
  1.         //绑定本地IP和端口
  2.         struct sockaddr_in ser;
  3.         bzero( &ser, sizeof(ser));
  4.         ser.sin_family = AF_INET;              //使用网络地址族
  5.         ser.sin_port = htons(SER_PORT);          //端口号9999
  6.         inet_aton(SER_IP, &ser.sin_addr);         //转IP地址
  7.         ret = bind( serfd, (struct sockaddr *)&ser, sizeof(ser));
  8.         if(ret < 0){
  9.                 perror("bind failed!\n");
  10.                 exit(1);
  11.         }
复制代码
5、设置为监听模式:套接字变为被动等待连接
  1.        //设置监听模式:套接字变为被动等待连接
  2.         ret = listen( serfd, 5);
  3.         if(ret < 0){
  4.                 perror("listen failed!\n");
  5.                 exit(1);
  6.         }
复制代码
6、等待客户端连接
  1.        //等待客户端连接
  2.         struct sockaddr_in cli;
  3.         bzero( &cli, sizeof(cli));
  4.         int len = sizeof(cli);
  5.         int newfd = -1;
  6.         newfd = accept( serfd, (struct sockaddr *)&cli, &len);
  7.         if( newfd < 0){
  8.                 perror("accept failed!\n");
  9.                 exit(1);
  10.         }
  11.         printf("newfd = %d ip = %s port = %d\n", newfd,\
  12.                         inet_ntoa(cli.sin_addr), ntohs(cli.sin_port));
复制代码
7、打开文件
  1.         //打开一个文件
  2.         int fd = -1;
  3.         fd = open("./1.txt", O_CREAT|O_WRONLY, 0666);
  4.         if(fd < 0){
  5.                 perror("open failed!\n");
  6.                 exit(1);
  7.         }
复制代码
8、接收数据保存到文件中,从发过来文件中一直读数据,每读127字节,文件指针在文件的指向会往后偏移,直至读完该文件结束while循环
  1.         //接收数据保存到文件中
  2.         while(1){
  3.                 bzero( buf, sizeof(buf));
  4.                 ret = read( newfd, buf, 127);
  5.                 if(ret < 0){
  6.                         perror("read failed!\n");
  7.                         exit(1);
  8.                 }
  9.                 if(ret == 0){
  10.                         printf("read over!\n");
  11.                         break;
  12.                 }
  13.                 printf( "%s\n", buf);
  14.                 write( fd, buf, ret);
  15.         }
复制代码
二、client端代码编写client端代码编写流程
        ①、添加头文件
        ②、建立流失套接字
        ③、主动连接服务器
        ④、打开一个文件
        ⑤、发送数据到服务器

1、添加头文件

  1. #include <string.h>
  2. #include <strings.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <fcntl.h>
  11. #include <netinet/in.h>
  12. #include <netinet/ip.h>
  13. #include <arpa/inet.h>
复制代码
2、建立流失套接字
  1.         //建立流失套接字
  2.         //参数1-----地址协议族,AF_INET网络协议族
  3.         //参数2-----套接字类型,SOCK_STREAM使用TCP协议
  4.         //参数3-----protocol参数通常设置0
  5.         //返回值----成功返回socket id,失败返回-1
  6.         clifd = socket( AF_INET, SOCK_STREAM, 0);
  7.         if(clifd < 0){
  8.                 perror("socket id failed!\n");
  9.                 exit(1);
  10.         }
复制代码
3、连接服务器
  1.         //主动连接服务器
  2.         struct sockaddr_in ser;
  3.         bzero( &ser, sizeof(ser));
  4.         ser.sin_family = AF_INET;
  5.         ser.sin_port = htons(9999);                //要连接的服务器端口
  6.         ser.sin_addr.s_addr = inet_addr(argv[1]);        //服务器IP地址
  7.         ret = connect( clifd, (struct sockaddr *)&ser, sizeof(ser));
  8.         if(ret < 0){
  9.                 perror("connect failed!\n");
  10.                 exit(1);
  11.         }
  12.         printf("connect server ok!\n");
复制代码
4、打开一个文件
  1.         //打开一个文件
  2.         int fd = open("./1.txt", O_RDONLY);
  3.         if(fd < 0){
  4.                 perror("open failed!\n");
  5.                 exit(1);
  6.         }
复制代码
5、发送数据到服务器,每次从文件中读128个数据出来写入套接字clifd中
  1.         //发送数据到服务器
  2.         while(1){
  3.                 bzero( buf, 128);
  4.                 //先从文件读数据到buf中
  5.                 ret = read( fd, buf, 127);
  6.                 if(ret < 0){
  7.                         perror("read 1.txt failed!\n");
  8.                         exit(1);
  9.                 }
  10.                 if(ret == 0){        //读到文件末尾
  11.                         printf("send ok!\n");
  12.                         break;
  13.                 }
  14.                
  15.                 ret = write( clifd, buf, sizeof(buf));
  16.                 if(ret < 0){
  17.                         perror("write file failed!\n");
  18.                         exit(1);
  19.                 }
  20.         }
复制代码

三、编译并允许代码使用gcc编译sever代码,使用交叉编译工具编译client代码
截图202103101811289703..png
先执行sever端代码。然后在板卡上执行client代码,传输1.txt文件,cat一下对比传输文件是否一致

截图202103101819007382..png
代码在附件中

tcp_core_file.tar.gz

7.78 KB, 下载次数: 128

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则