#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <head.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 69 //端口号:填服务器绑定端口号
#define IP "192.168.1.101" //IP地址:windows的IP地址
int upload(int cfd,struct sockaddr_in sin)
{
char filename[20] = "";
printf("请输入要上传的文件名>>> ");
scanf("%s", filename);
while(getchar()!=10);
//判断文件存不存在
int fd = open(filename,O_RDONLY);
if(fd < 0)
{
ERR_MSG("open");
return -1;
}
//发送数据
char buf[516] = "";
int size = sprintf(buf, "%c%c%s%c%s%c", 0, 2, filename, 0, "octet", 0);
if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
socklen_t addrlen = sizeof(sin);
ssize_t res = 0;
unsigned short num = 0; //本地记录的块编号
while(1)
{
bzero(buf, sizeof(buf));
//接收服务器的应答
res = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
break;
}
if(4 == buf[1])
{
if(htons(num) == *(unsigned short*)(buf+2))
{
//组数据包给服务器
num++;
*(unsigned short*)buf = htons(3);
*(unsigned short*)(buf+2) = htons(num);
res = read(fd, buf+4, 512);
if(res < 0)
{
break;
}
else if(0 == res)
{
printf("文件:%s 上传完毕\n", filename);
break;
}
//发送数据包
if(sendto(cfd, buf, res+4, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
}
}
else if(5 == buf[1])//错误包
{
printf("MSG_ERR: code[%d] msg[%s] __%d__\n", \
ntohs(*(unsigned short*)(buf+2)), buf+4, __LINE__);
break;
}
}
close(fd);
return 0;
}
int download(int cfd,struct sockaddr_in sin)
{
char buf[516] = "";
char filename[20] = "";
printf("请输入要下载的文件名>>> ");
scanf("%s", filename);
while(getchar()!=10);
//发送下载请求
//组协议包
int size = sprintf(buf, "%c%c%s%c%s%c", 0, 1, filename, 0, "octet", 0);
//sendto
if(sendto(cfd, buf, size, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
//本地创建并打开要下载的文件
int fd = -1; //必须初始化一个无意义的文件描述符,否则下面的close
socklen_t addrlen = sizeof(sin);
ssize_t res = 0;
unsigned short num = 0; //本地记录的块编号
//循环接收数据包,回复ACk
while(1)
{
bzero(buf, sizeof(buf));
//接收数据包
res = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
break;
}
if(3 == buf[1]) //数据包
{
if(htons(num+1) == *(unsigned short*)(buf+2))
{
num++;
if(-1 == fd)
{
fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664);
if(fd < 0)
{
ERR_MSG("open");
break;
}
}
//将获取到的数据,写入到文件中
if(write(fd, buf+4, res-4) < 0)
{
ERR_MSG("write");
break;
}
buf[1] = 4;
if(sendto(cfd, buf, 4, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
break;
}
if(res-4 < 512)
{
printf("======= 文件下载完毕 =======\n");
break;
}
}
}
else if(5 == buf[1])//错误包
{
printf("MSG_ERR: code[%d] msg[%s] __%d__\n", \
ntohs(*(unsigned short*)(buf+2)), buf+4, __LINE__);
break;
}
}
close(fd);
return 0;
}
int main(int argc, const char *argv[])
{
//创建报式套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("cfd = %d\n",cfd);
//绑定客户端的地址信息结构体到套接字上--->非必须绑定
//若不绑定,则操作系统会给客户端绑定上客户端所在的主机IP以及随机端口(49151~65535)
//填充地址信息结构体,真实的地址信息结构体根据地址族制定
//AF_INET:man 7 ip
//要连接哪个服务器,就填对应服务器的IP和端口
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET
sin.sin_port = htons(PORT); //端口号:填服务器绑定端口号
sin.sin_addr.s_addr = inet_addr(IP); //IP地址:服务器绑定的IP地址
char c = 0;
while(1)
{
printf("-------------------------\n");
printf("---------1.下载----------\n");
printf("---------2.上传----------\n");
printf("-------------------------\n");
printf("请输入>>> ");
c = getchar();
while(getchar()!=10);
switch(c)
{
case '1' :
download(cfd,sin);
break;
case '2' :
upload(cfd,sin);
break;
default :
printf("输入错误,请重新输入\n");
break;
}
while(getchar()!=10);
}
//关闭所有文件描述符
close(cfd);
return 0;
}
文章来源地址https://www.toymoban.com/news/detail-646121.html
文章来源:https://www.toymoban.com/news/detail-646121.html
到了这里,关于网络编程(TFTP协议实验)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!