tftp.c
#include "tftp.h"
#include <myhead.h>
//菜单
void menu(int *flag)
{
printf("——————————————————————————\n");
printf("——————————上传:1——————————\n");
printf("——————————下载:2——————————\n");
printf("——————————退出:0——————————\n");
printf("——————————————————————————\n");
scanf("%d",flag);
}
//下载
int downlod(int sfd,struct sockaddr_in sin)
{
char cuf[512] = "";
char brr[128] = "";
printf("请输入下载的文件-------->\n");
scanf("%s", brr);
short *ptr1 = (short *)cuf;
*ptr1 = htons(1);
char *ptr2 = cuf + 2;
strcpy(ptr2, brr);
char *ptr4 =ptr2+strlen(ptr2)+1;
strcpy(ptr4,"octet");
int len = 2+strlen(brr) + 1 + strlen(ptr4) + 1;
if(sendto(sfd,cuf,len,0,(struct sockaddr*)&sin,sizeof(sin)) < 0 )
{
ERR_MSG("sendto");
return-1;
}
//接受返回的地址信息结构体
struct sockaddr_in cin;
socklen_t clen = sizeof(cin);
char buf[512];//数据包
short * pcr1 = (short *)buf;
*pcr1 = htons(3);
short * pcr2 = pcr1 + 1;
char arr[4];//ack
short * ptrc1 = (short *)arr;
*ptrc1 = htons(4);
short * ptrc2 = ptrc1 + 1;
ssize_t ter = 0;
int fp = open("./5.png", O_WRONLY|O_CREAT|O_APPEND|O_TRUNC,0664);
while(1)
{
//接受数据
ter = recvfrom(sfd,buf,516,0,(struct sockaddr*)&cin,&clen);
printf("ter=%ld __%d__\n", ter, __LINE__);
*ptrc2 = *pcr2;
write(fp,&buf[4], ter-4);
sendto(sfd,arr,sizeof(arr), 0, (struct sockaddr*)&cin,sizeof(cin));
if(ter < 512)
{
printf("下载成功\n");
break;
}
}
return 0;
}
int upload(int sfd,struct sockaddr_in sin)
{
int ph = open("./5.png", O_RDONLY);
char cuf[512] = "";
char brr[200] = "";
printf("-------->请输入传输的文件\n");
scanf("%s", brr);
short *ptr1 = (short *)cuf;
*ptr1 = htons(2); //写请求
char *ptr2 = cuf + 2;
strcpy(ptr2, brr);
char *ptr4 =ptr2+strlen(ptr2)+1;
strcpy(ptr4,"octet");
int len = 2+strlen(brr) + 1 + strlen(ptr4) + 1;
if(sendto(sfd,cuf,len,0,(struct sockaddr*)&sin,sizeof(sin)) < 0 )
{
ERR_MSG("sendto");
return-1;
}
//打包
char demo[512] = "";
short * ctr = (short*) demo;
*ctr = htons(3);
short* ctr2 = ctr+1;
char arr[4];//ack
short * ptrc1 = (short *)arr;
*ptrc1 = htons(4);
short * ptrc2 = ptrc1 + 1;
ssize_t tev = 0, ter = 0;;
int i = 1;
//定义接收的临时套接字
struct sockaddr_in cin;
socklen_t clen = sizeof(cin);
while(1)
{
//发送数据
if(recvfrom(sfd,arr,sizeof(arr),0,(struct sockaddr*)&cin,&clen)<0)//收到服务器创建的临时套接字地址
{
ERR_MSG("recvfrom");
return -1;
}
*ctr2 = htons(i++);//编号
tev = read(ph,(char*)(demo+4), 512);//读取文件
if(ter = sendto(sfd, demo,tev+4,0, (struct sockaddr*)&cin, sizeof(cin)) < 0)//发送文件
{
ERR_MSG("sendto");
return -1;
}
if(tev < 512)
{
printf("发送成功\n");
break;
}
printf("ter = %ld __%d__\n", ter, __LINE__);
}
return 0;
}
tftp.h
#ifndef TFTP_H
#define TFTP_H
#include <myhead.h>
//菜单
void menu(int *flag);
//上传
int download(int sfd,struct sockaddr_in sin);
//下载
int upload(int sfd,struct sockaddr_in sin);
#endif
main.c
#include "tftp.h"
#include <myhead.h>
int main(int argc, char const *argv[])
{
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("sfd");
return -1;
}
printf("socket create succes sfd=%d __%d__\n", sfd, __LINE__);
//允许端口快速被复用---监测端口号是否真的有进程在占用,如果没有,则快速复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速复用成功\n");
//定义并填充地址信息结构体,方便发送数据
//真实的地址信息结构体根据地址族指定 AF_INET :man ip(7)
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT); //端口号的网络字节序 : 1024-49151
sin.sin_addr.s_addr = inet_addr(IP); //本机号的网络字节序列
char choose = 0;
while(1)
{
printf("-------------------------\n");
printf("----------1.下载---------\n");
printf("----------2.上传---------\n");
printf("----------3.退出---------\n");
printf("-------------------------\n");
printf("请输入---->");
choose = getchar();
while(getchar()!= '\n');
switch(choose)
{
case '1' :
//下载
do_download(sfd, sin);
break;
case '2':
//上传 do_upload
do_upload(sfd, sin);
break;
case '3':
close(sfd);
return 0;
//退出
default:
printf("输入错误请重新输出\n");
}
}
//关闭套接字
if(close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}