在Linux上用C语言编写tftpd32的客户端,实现上传和下载文件功能。
代码实现如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n", __LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.8.227" //服务器IP
#define PORT 69 //1024 - 49151
void file_download(int sfd, struct sockaddr_in sin);
void file_upload(int sfd, struct sockaddr_in sin);
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//填充地址信息结构体
//根据地址族的不同,地址信息结构体不一致,AF_INET:man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET
sin.sin_port = htons(PORT); //端口号的网络字节序,1024-49151
sin.sin_addr.s_addr = inet_addr(IP); //本机IP地址的网络字节序
//更换网络环境后IP地址可能改变
while(1)
{
//客户端界面
printf("*******************\n");
printf("******1.下载*******\n");
printf("******2.上传*******\n");
printf("******3.退出*******\n");
printf("*******************\n");
int option;
printf("请输入>>>");
scanf("%d", &option);
switch(option)
{
case 1:
file_download(sfd, sin);
break;
case 2:
file_upload(sfd, sin);
break;
case 3:
goto END;
break;
}
}
END:
//关闭套接字
close(sfd);
return 0;
}
void file_download(int sfd, struct sockaddr_in sin)
{
socklen_t addrlen = sizeof(sin);
char buf[600] = "";
ssize_t res = 0;
short code = 0; //用来保存操作码
short num = 0; //用来保存块编号 或 差错码
int block_num = 0; //用于校验已经接到的数据包块编号
int fd = 0;
char filename[32] = {0};
printf("请输入要下载的文件名>>>");
scanf("%s", filename);
//组下载协议
int size = sprintf(buf, "%c%c%s%c%s%c", 0, 1, filename, 0, "octet", 0);
//发送下载协议
if(sendto(sfd, buf, size, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return ;
}
while(1)
{
//接收数据
res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return ;
}
//解析操作码
code = ntohs(*(short*)buf);
//解析块编号 或 差错码
num = ntohs(*(short*)(buf+2));
if(5 == code)
{
printf("errno = [%d] errstr = [%s]\n", num, buf+4);
break;
}
if(3 == code && block_num+1 == num)
{
block_num++;
if(1 == num) //只有接收第一个数据包时打开文件
{
fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664);
if(fd < 0)
{
ERR_MSG("open");
return ;
}
}
//将文件内容写入文件
if(write(fd, buf+4, res-4) < 0)
{
ERR_MSG("write");
return ;
}
bzero(buf, sizeof(buf));
//组装ACK
*(short*)buf = htons(4);
*(short*)(buf+2) = htons(num);
//发送ACK
if(sendto(sfd, buf, 4, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return ;
}
if(res-4 < 512)
{
printf("file download success\n");
break;
}
}
}
//关闭文件
close(fd);
return ;
}
void file_upload(int sfd, struct sockaddr_in sin)
{
socklen_t addrlen = sizeof(sin);
char buf[600] = "";
ssize_t res = 0; //接recvfrom的返回值
short code = 0; //用来保存操作码
short num = 0; //用来保存块编号 或 差错码
int block_num = 0; //用于校验已经接到的数据包块编号
int fd = 0; //文件描述符
char filename[32] = {0};
printf("请输入要上传的文件名>>>");
scanf("%s", filename);
//打开文件
fd = open(filename, O_RDONLY);
if(fd < 0)
{
ERR_MSG("open");
return ;
}
//组上传协议
int size = sprintf(buf, "%c%c%s%c%s%c", 0, 2, filename, 0, "octet", 0);
//发送上传协议
if(sendto(sfd, buf, size, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return ;
}
while(1)
{
//接收数据
res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return ;
}
//解析ACK操作码
code = ntohs(*(short*)buf);
//解析ACK块编号 或 差错码
num = ntohs(*(short*)(buf+2));
if(4 == code && block_num == num)
{
block_num++;
bzero(buf, sizeof(buf));
//组数据包
*(short*)buf = htons(3);
*(short*)(buf+2) = htons(num+1);
res = read(fd, buf+4, 512);
if(res < 0)
{
ERR_MSG("write");
return ;
}
//发送数据包
if(sendto(sfd, buf, res+4, 0, (struct sockaddr*)&sin, addrlen) < 0)
{
ERR_MSG("sendto");
return ;
}
if(res < 512)
{
printf("file upload success\n");
break;
}
}
}
//关闭文件
close(fd);
return ;
}
运行结果如下: