4/11日作业

news/2024/5/18 13:17:22 标签: udp, 网络协议, 网络

1、基于UDP的TFTP文件传输

  1 #include<stdio.h>
  2 #include<sys/types.h>
  3 #include<sys/socket.h>
  4 #include<netinet/in.h>
  5 #include<netinet/ip.h>
  6 #include<string.h>
  7 #include<arpa/inet.h>
  8 #include<unistd.h>
  9 #include<sys/stat.h>
 10 #include<fcntl.h>
 11 #define ERR_MSG(msg) do{\
 12     fprintf(stderr,"%d",__LINE__);\
 13     perror("msg");\
 14 }while(0)
 15 
 16 #define SER_IP "192.168.2.131"
 17 #define SER_PORT 69
 18 
 19 #define CLI_IP "192.168.0.111"
 20 #define CLI_PORT 7777
 21 
 22 
 23 void Download();
 24 
 25 void Upload()
 26 {
 27     while(1)
 28         sleep(1);
 29 }
 30 
 31 
 32 
 33 int main(int argc, const char *argv[])
 34 {
 35     int option =-1;
 36     while(1)
 37     {
 38         printf("************************\n");
 39         printf("********1、下载*********\n");
 40         printf("********2、上传*********\n");
 41         printf("********3、退出*********\n");
 42         printf("************************\n");
 43         scanf("%d",&option);
 44         while(getchar() != 10);
 45         if(1 == option)
 46             Download();
 47         else if(2 == option)
 48             Upload();
 49         else if(3 == option)
 50             break;
 51     }
 52 
 53 
 54 
 55     /*
 56     ssize_t res = 0;
 57     
 58     while(1)
 59     {
 60         bzero(buf,sizeof(buf));
 61         printf("请输入>>>");
 62         fgets(buf,sizeof(buf),stdin);
 63         buf[strlen(buf)-1] = 0;
 64 
 65 
 66         //发送数据
 67         if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
 68         {
 69             ERR_MSG("sendto");
 70             return -1;
 71         }
 72         printf("发送成功\n");
 73 
 74         //接收数据
 75             error_message(*(short*)(buf+2));
 76         bzero(buf,sizeof(buf));
 77         //继续接受数据包发送方的地址信息,给sendto使用
 78         res = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&rcvaddr,&addrlen);
 79         if(res < 0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
 80         {
 81             ERR_MSG("recvfrom");
 82             return -1;
 83         }
 84         printf("[%s:%d]: %s\n",inet_ntoa(rcvaddr.sin_addr),ntohs(rcvaddr.sin_port),buf);
 85 
 86     }
 87     */
 88 
 89     //关闭
 90     return 0;
 91 }
 92 
 93 
 94 void Download()
 95 {
 96     //创建报式套接字
 97     int cfd = socket(AF_INET,SOCK_DGRAM,0);
 98     if(cfd < 0)
 99     {
100         ERR_MSG("socket");
101         return;
102     }
103 
104     char buf[516]={0};
105 
106     //创建填充服务器地址信息结构体
107     struct sockaddr_in sin;
108     sin.sin_family      = AF_INET;
109     sin.sin_port        = htons(SER_PORT);
110     sin.sin_addr.s_addr = inet_addr(SER_IP);
111 
112     socklen_t addrlen = sizeof(sin);
113 
114     //设置读请求
115     *(short*)buf = htons(1);
116     //printf("%hd\t__%d__\n",ntohs(*(short*)buf),__LINE__);
117 
118     //将文件名写入到buf中
119     printf("请输入要下载的文件名>>>>>>\n");
120     scanf("%s",buf+2);
121     while(getchar() != 10);
122     char pathname[128]="";
123     strcpy(pathname,buf+2);
124 
125     strcpy(buf+2+strlen(buf+2)+1,"octet");
126 
127     printf("%hd %s %s\n",ntohs(*(short*)buf),buf+2,buf+2+strlen(buf+2)+1);
128 
129 
130 
131 
132     char *p1 = buf+2+strlen(buf+2)+1;
133     int size = 4 + strlen(buf+2)+strlen(p1)+1;
134     //发送数据
135     if(sendto(cfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
136     {
137         ERR_MSG("sendto");
138         return;
139     }
140     printf("发送成功   size=%d\n",size);
141 
142     ssize_t res = 0;
143     short count=1;
144 
145     int fd = -1;;
146     while(1)
147     {
148         //接收数据
149         bzero(buf,sizeof(buf));
150         res = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
151         if(res < 0)
152         {
153             ERR_MSG("recvfrom");
154             return;
155         }
156         //  printf("[%s:%d]: %s\n",inet_ntoa(rcvaddr.sin_addr),ntohs(rcvaddr.sin_port),buf);
157         //  printf("%d : %d\t%s\n",ntohs(*(short*)buf),ntohs(*(short*)(buf+2)),buf+4);
158         if(ntohs(*(short*)buf) == 3)
159         {
160             if(1 == ntohs(*(short*)(buf+2)))
161             {
162                 //创建文件
163                 fd = open(pathname,O_WRONLY|O_CREAT|O_TRUNC,0664);
164                 if(fd < 0)
165                 {
166                     ERR_MSG("open");
167                     return;
168                 }
169             }
170 
171             if(ntohs(*(short*)(buf+2)) == count)
172             {
173                 write(fd,buf+4,res-4);
174                 if(res < 0)
175                 {
176                     ERR_MSG("read");
177                     break;
178                 }
179                 else if(res < 512)
180                 {
181                     printf("Download sucess\n");
182                     break;
183                 }
184                 //发送数据
185                 bzero(buf,sizeof(buf));
186                 *(short *)buf = htons(4);
187                 *((short *)(buf+2))=htons(count++);
188 
189                 if(sendto(cfd,buf,4,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
190                 {
191                     ERR_MSG("sendto");
192                     return ;
193                 }
194             }
195         }
196         else if(ntohs(*(short *)buf) == 5)
197         {
198             printf("%s  __%d__\n",buf+4,__LINE__);
199             break;
200         }
201         //  printf("发送成功\n");
202 
203     }
204 

ubuntu@ubuntu:TFTP$ gcc 02_udpCli_TFTP.c 
ubuntu@ubuntu:TFTP$ ./a.out 
************************
********1、下载*********
********2、上传*********
********3、退出*********
************************
1
请输入要下载的文件名>>>>>>
5.png
1 5.png octet
发送成功   size=15
Download sucess
************************
********1、下载*********
********2、上传*********
********3、退出*********
************************
^C
ubuntu@ubuntu:TFTP$ ls
02_udpCli_TFTP.c  2       5      a.out  asd.c     d         huahua.c
1.png             2octet  5.png  asd    asdoctet  dict.txt
ubuntu@ubuntu:TFTP$ eog 5.png
ubuntu@ubuntu:TFTP$ 
 

 


http://www.niftyadmin.cn/n/211342.html

相关文章

Spring Boot 整合 Swagger 教程详解

✅作者简介&#xff1a;2022年博客新星 第八。热爱国学的Java后端开发者&#xff0c;修心和技术同步精进。 &#x1f34e;个人主页&#xff1a;Java Fans的博客 &#x1f34a;个人信条&#xff1a;不迁怒&#xff0c;不贰过。小知识&#xff0c;大智慧。 &#x1f49e;当前专栏…

mysql【查操作(上)】

mysql查操作(上) select在sql中是很重要的操作了 use school;-- 01. 查询所有学生的所有信息 select * from students; -- *效率比较低-- 数据保存在sys中的cloum中的 -- 投影 select stu_id,stu_name,stu_sex,stu_birth,stu_addr,col_id from students;-- 使用这个格式&…

前端如何处理文本溢出

前言 在现代网页设计中&#xff0c;文本是网页中最重要的内容之一。然而&#xff0c;当文本超出其容器的大小时&#xff0c;会发生文本溢出的问题。文本溢出不仅会影响网页的视觉效果&#xff0c;还会影响网页的可读性和可用性。在前端开发中&#xff0c;解决文本溢出的问题是…

初识Spring

1、Spring是什么 Spring是一个分层的Java SE/EE full-stack&#xff08;一站式&#xff09;轻量级开源框架&#xff0c;以 IOC&#xff08;Inverse Of Control&#xff1a;反转控制&#xff09;和 AOP&#xff08;Aspect Oriented Programming&#xff1a;面向切面编程&#xf…

认证服务---OAuth2.0基本介绍,微博登录整合到实际项目中【下篇】

前言 上一篇简单介绍了它的基本使用&#xff0c;这一篇就粗略说明一下如何在项目中实际应用 1、核心代码 1.1 认证微服务 当你进行了授权之后&#xff0c;跳转到一个新的地址。这个地址应该是你访问接口的地址。在这个接口中完成相应的access_token获取&#xff0c;以及调用…

面试技巧大全

目录 一、面试前的准备 1&#xff0e;确定 2&#xff0e;知己知彼 3&#xff0e;披挂上阵 4&#xff0e;心理战术 二、面试种类 1&#xff0e;按人员编排分类:招聘人员→应聘人员 2&#xff0e;按阶段分类 3.按型式分类 三、面试程序 1&#xff0e;寒喧、问候Greeti…

自动化测试mock模块使用详解介绍

mock简介 py3已将mock集成到unittest库中为的就是更好的进行单元测试简单理解&#xff0c;模拟接口返回参数通俗易懂&#xff0c;直接修改接口返回参数的值官方文档&#xff1a;unittest.mock --- 模拟对象库 — Python 3.11.3 文档mock作用 解决依赖问题&#xff0c;达到解耦…

数据库学习笔记-----SQL查询语句和代码演示

SQL不区分大小写&#xff0c;本文是邹兆年老师的课件和课堂的部分内容总结&#xff0c;部分比较细的内容请看课件 Db笔记(1).pdf SQL的数据定义 数据 数值型&#xff1a; 整型&#xff1a;INT/SMALLINT/BIGINT 4个字节/两个字节/八个字节浮点型&#xff1a;NUMERIC…