网络编程0608

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define ERR_MSG(msg) do{\
		fprintf(stderr,"line;%d;%s %s",__LINE__,__FILE__,__func__);\
		perror(msg);\
}while(0)
#define IP "192.168.2.84"
#define PORT 69
int xia_zai(int cfd,struct sockaddr_in sin)
{
	char name[20]="";
	char buf[516]={0};
	printf("请输入要下载的文件名>");
	scanf("%s",name);
	while(getchar()!=10);
	//printf("%s",name);
//===============================
	short*p1=(short*)buf;
	*p1 = htons(1);
//==============================
	char *p2 = buf+2;
	strcpy(p2,name);
//=============================
	char *p4 = p2+strlen(p2)+1;
	strcpy(p4,"octet");
//===============================
int len = 4+strlen(p2)+strlen(p4);
printf( "%c%c%s%c%s%c\n", 0, 1, name,0, "octet", 0);
if(sendto(cfd, buf, len, 0, (struct sockaddr*)&sin, sizeof(sin))<0)
{
	ERR_MSG("sendto");
	return -1;
}
printf("发送成功\n");

//变量最好初始化,且初始化为无意义的数据
int fd = -1;
socklen_t addrlen = sizeof(sin);
ssize_t res = 0;
int ret = 0;
unsigned short num = 0;
while (1)
{
	bzero(buf, sizeof(buf));
	//接收数据包
	res = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);
	printf("%d\n",buf[1]);
	if (res < 0)
	{
		ERR_MSG("recvfrom");
		ret = -1;
		break;
	}
	//下列测试可以得到结果,有效操作码存储在buf[1]的位置,buf[0]的位置是0
	//printf("操作码:%#x | %d %d\n", ntohs(*(short*)buf), buf[0], buf[1]);
	//printf("    快编号:%d | %d %d\n", ntohs(*(short*)(buf+2)), buf[2], buf[3]);

	if (3 == buf[1])     //数据包
	{
		//判断数据包中的块编号是否是正确的块编号
		//需要与本地记录的块编号进行对比
		if (*(unsigned short*)(buf + 2) == htons((num + 1)))
		{
			num++;
			if (1 == ntohs(*(unsigned short*)(buf + 2)))
			{
				fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0664);
				if (fd < 0)
				{
					ERR_MSG("open");
					return -1;
				}
			}

			//将接收到的数据另存
			if (write(fd, buf + 4, res - 4) < 0)
			{
				ERR_MSG("write");
				ret = -1;
				break;
			}


			//回复ACK,由于ACK包和数据包前四个字节只有操作码不一致
			//所以可以直接修改数据包的操作码,然后将前4个字节回复给服务器即可
			buf[1] = 4;
			if (sendto(cfd, buf, 4, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
			{
				ERR_MSG("sendto");
				ret = -1;
				break;
			}

			//直到数据小于512个字节,实际获取到的数据包大小为res
			//数据大小= res - 操作码 - 块编号
			if (res - 4 < 512)
			{
				printf("%s 文件拷贝完毕\n", name);
				break;
			}
		}
	}
	else if (5 == buf[1])    //错误包
	{
		fprintf(stderr, "DOWNLOAD_ERROR:%d : %s\n", ntohs(*(short*)(buf + 2)), buf + 4);
		break;
	}
}

close(fd);
return ret;



}
int main(int argc, const char *argv[])
{
	//创建报式套接字
	int cfd = socket(AF_INET,SOCK_DGRAM,0);
	if(cfd < 0)
	{		
		ERR_MSG("socket");
		return -1;
	}
	printf("create soccess sfd=%d __%d++\n",cfd,__LINE__);
	//填充服务器的地址信息结构体,给bind函数绑定
	
	//真实的地址信息结构体柑橘地址族定制
	struct sockaddr_in sin;
	
		sin.sin_family  =AF_INET;//必须填AF_TNET;
		sin.sin_port    = htons(PORT);//1024-49151,网络字节序
		sin.sin_addr.s_addr = inet_addr(IP);//ifconfig出来本机ip
		

		char c=0;
		while(1)
		{
			printf("---------------------------\n");
			printf("----------1. 下载----------\n");
			printf("----------2. 上传----------\n");
			printf("----------3. 退出----------\n");
			printf("---------------------------\n");	
			c = getchar();
			while(getchar() != 10);
			switch(c)
			{
			case '1':
				xia_zai(cfd, sin);
				break;
			case '2':
				
				break;
		//	case '3':
		//		goto END;
		//
		//break;
			default:
				printf("输入错误,请重新输入\n");
			}

			printf("输入任意字符清屏>>>");
			while(getchar()!=10);

		}

		//关闭套接字文件描述符


		close(cfd);
		return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值