4月22作业

文件下载

  1	#include<string.h>
     2	#include<stdlib.h>
     3	#include<stdio.h>
     4	#include <sys/types.h>	       /* See NOTES */
     5	#include <sys/socket.h>
     6	#include <netinet/in.h>
     7	#include <arpa/inet.h>
     8	#include <unistd.h>
     9	#include <sys/types.h>
    10	#include <sys/stat.h>
    11	#include <fcntl.h>
    12	#define ERR_MSG(a) do{\
    13		fprintf(stderr,"line: %d\n",__LINE__);\
    14		perror(a);\
    15		}while(0)
    16	#define PORT 69
    17	#define IP "192.168.0.129"
    18	int main(int argc, const char *argv[])
    19	{
    20		//创建套接字
    21		int cfd = socket(AF_INET,SOCK_DGRAM,0);
    22		if(cfd < 0)
    23		{
    24			ERR_MSG("socket");
    25			return -1;
    26		}
    27		struct sockaddr_in sin;
    28		sin.sin_family = AF_INET;   //ipv4
    29		sin.sin_port  = htons(PORT);//端口号的网络字节序
    30		sin.sin_addr.s_addr = inet_addr(IP);//服务器IP地址
    31	
    32		//组下载请求协议
    33		
    34		char buf[516] = "";
    35		short* p1 = (short*)buf;
    36		*p1 = htons(1);
    37	
    38		char* p2 = (char*)(p1 + 1);
    39		printf("请输入下载的文件>>>");
    40		char buf1[20] = "";
    41		scanf("%s",buf1);
    42		strcpy(p2,buf1);
    43	
    44		char* p3 = p2+strlen(p2);
    45		*p3 = 0;
    46		char* p4 = p3 + 1;
    47		strcpy(p4,"octet");
    48		char* p5 = p4 + strlen(p4);
    49		*p5 = 0;
    50		int size = 2+1+1+strlen(p2)+strlen(p4);
    51	
    52		//发送下载请求
    53		if(sendto(cfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
    54			{
    55				ERR_MSG("sendto");
    56				return -1;
    57			}
    58		printf("发送成功\n");
    59	
    60		struct sockaddr_in cin;//接受数据包地址信息结构体
    61		socklen_t  addrlen = sizeof(cin);
    62	
    63		ssize_t res;//存储vecvfrom返回值
    64	
    65		//打开一个文件
    66		int fd = open(buf1,O_WRONLY|O_CREAT|O_TRUNC,0664);//打开一个文件
    67		if(fd < 0)
    68		{
    69			ERR_MSG("open");
    70			return -1;
    71		}
    72		printf("open success\n");
    73		
    74	
    75		while(1)
    76		{
    77			memset(buf,0,sizeof(buf));//清空
    78			//接受数据包
    79			res = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);//接收数据包
    80			if(res < 0)
    81			{
    82				ERR_MSG("recvfrom");
    83				return -1;
    84			}
    85			if(*(buf+2) == 0x05)
    86			{
    87				printf("11111%s\n",(buf+4));
    88				break;
    89			}
    90	
    91			printf("recvfrom success\n");
    92			//将数据写入文件中
    93			printf("res = %ld\n",res);
    94			if(write(fd,buf+4,res-4) < 0)
    95			{
    96				ERR_MSG("write");
    97				return -1;
    98			}
    99			
   100			printf("write success\n");
   101	
   102			*(buf+1) = 0x04;//重置操作码
   103	
   104			//封装继续包ACK
   105	
   106			if(sendto(cfd,buf,4,0,(struct sockaddr*)&cin,addrlen) < 0)//发送ACK
   107			{
   108				ERR_MSG("sendto");
   109				return -1;
   110			}
   111	
   112			if(res < 516)//接收数据小于516下载完成
   113			{
   114				printf("下载完成\n");
   115				break;
   116			}
   117		}
   118	
   119		close(fd);
   120		close(cfd);
   121		return 0;
   122	}

文件上传

     1	#include<string.h>
     2	#include<stdlib.h>
     3	#include<stdio.h>
     4	#include <sys/types.h>	       /* See NOTES */
     5	#include <sys/socket.h>
     6	#include <netinet/in.h>
     7	#include <arpa/inet.h>
     8	#include <unistd.h>
     9	#include <sys/types.h>
    10	#include <sys/stat.h>
    11	#include <fcntl.h>
    12	#define ERR_MSG(a) do{\
    13		fprintf(stderr,"line: %d\n",__LINE__);\
    14		perror(a);\
    15		}while(0)
    16	#define PORT 69
    17	#define IP "192.168.0.129"
    18	int main(int argc, const char *argv[])
    19	{
    20		//创建套接字
    21		int cfd = socket(AF_INET,SOCK_DGRAM,0);
    22		if(cfd < 0)
    23		{
    24			ERR_MSG("socket");
    25			return -1;
    26		}
    27		struct sockaddr_in sin;
    28		sin.sin_family = AF_INET;   //ipv4
    29		sin.sin_port  = htons(PORT);//端口号的网络字节序
    30		sin.sin_addr.s_addr = inet_addr(IP);//服务器IP地址
    31	
    32		//组下载请求协议
    33		
    34		char buf[516] = "";
    35		short* p1 = (short*)buf;
    36		*p1 = htons(2);
    37	
    38		char* p2 = (char*)(p1 + 1);
    39		printf("请输入上传的文件>>>");   
    40		char buf1[20] = "";
    41		scanf("%s",buf1);
    42		strcpy(p2,buf1);
    43	
    44		char* p3 = p2+strlen(p2);
    45		*p3 = 0;
    46		char* p4 = p3 + 1;
    47		strcpy(p4,"octet");
    48		char* p5 = p4 + strlen(p4);
    49		*p5 = 0;
    50		int size = 2+1+1+strlen(p2)+strlen(p4);
    51	
    52		//发送下载请求
    53		if(sendto(cfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
    54			{
    55				ERR_MSG("sendto");
    56				return -1;
    57			}
    58		
    59		printf("发送成功\n");
    60	
    61		struct sockaddr_in cin;//接受数据包地址信息结构体
    62		socklen_t  addrlen = sizeof(cin);
    63	
    64		ssize_t res;//存储vecvfrom返回值
    65	
    66		//打开一个文件
    67		int fd = open(buf1,O_RDONLY);//打开一个文件
    68		if(fd < 0)
    69		{
    70			ERR_MSG("open");
    71			return -1;
    72		}
    73		printf("open success\n");
    74		
    75	
    76		while(1)
    77		{
    78			memset(buf,0,sizeof(buf));//清空
    79			//接受数据包
    80			if(recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen) < 0)//接收数据包
    81			{
    82				ERR_MSG("recvfrom");
    83				return -1;
    84			}
    85		/*	if(*(buf+2) == 0x05)
    86			{
    87				printf("11111%s\n",(buf+4));
    88				break;
    89			}
    90	*/		
    91			
    92	
    93			printf("recvfrom success\n");
    94			//读取数据
    95			size_t res = 0;
    96	
    97			*(buf+1) = 0x03;//重置操作码
    98			short* p6 = (short*)(buf+2);
    99			*p6 = ntohs(*p6) + 1;
   100			*p6 = htons(*p6);
   101	
   102	
   103	
   104			res = read(fd,buf+4,512);
   105			if(res < 0)
   106			{
   107				ERR_MSG("read");
   108				return -1;
   109			}
   110	
   111			printf("read success\n");
   112	
   113				//发送数据包
   114	
   115			if(sendto(cfd,buf,4+res,0,(struct sockaddr*)&cin,addrlen) < 0)//发送数据包
   116			{
   117				ERR_MSG("sendto");
   118				return -1;
   119			}
   120	
   121			if(res == 0)//上传数据是否等于0
   122			{
   123				printf("上传完成\n");
   124				break;
   125			}
   126		}
   127	
   128		close(fd);
   129		close(cfd);
   130		return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值